How to make an htaccess rule from id to name

I have a list of unique data:

Suppose I have the following data:

id name 1 Jhon 2 Peter 3 Mark 4 Scotty 5 Marry 

I am making the .htaccess rule for id :

 RewriteRule brandlisting/(.*)/ site/brandlisting?id=$1 [L] RewriteRule brandlisting/(.*) site/brandlisting?id=$1 [L] 

my url is:

 http://localhost/mate/admin/site/brandlisting/3 

this works for id.

Now I need a .htaccess rule for the name, so I create a rule for it:

 RewriteRule brandlisting/(.*)/ site/brandlisting?name=$1 [L] RewriteRule brandlisting/(.*) site/brandlisting?name=$1 [L] http://localhost/mate/admin/site/brandlisting/Mark 

When I used the above URL, I encountered the following error in the console:

"NetworkError: 400 Bad Request - http: // localhost / mate / admin / site / brandlisting / Mark "

and in the browser it shows:

Error 400 Your request is invalid.

My current .htaccess file

 RewriteEngine on RewriteCond %{HTTP_HOST} ^localhost/mate/admin$ [NC,OR] RewriteCond %{HTTP_HOST} ^localhost/mate/admin$ RewriteCond %{REQUEST_URI} !wordpress/ RewriteRule (.*) /wordpress/$1 [L] Options +FollowSymLinks -MultiViews RewriteEngine On RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$0 [PT,L] #RewriteRule brandlisting/(.*)/ site/brandlisting?id=$1 [L] #RewriteRule brandlisting/(.*) site/brandlisting?id=$1 [L] RewriteRule brandlisting/(.*)/ site/brandlisting?name=$1 [L] RewriteRule brandlisting/(.*) site/brandlisting?name=$1 [L] 
+7
php url-rewriting .htaccess mod-rewrite
source share
4 answers

.htaccess rules rely on order. If you plan on using multiple routes, save your htaccess rules and put your routes in PHP using one of several routing frameworks already written.

This explains why your .htaccess file does not work, line by line:

 RewriteEngine on 

This included RewriteEngine. There are no problems at the moment.

 RewriteCond %{HTTP_HOST} ^localhost/mate/admin$ [NC,OR] RewriteCond %{HTTP_HOST} ^localhost/mate/admin$ RewriteCond %{REQUEST_URI} !wordpress/ 

Just a match RewriteRule is WordPress. This seems to work, so let it ignore this rule block.

 RewriteRule (.*) /wordpress/$1 [L] Options +FollowSymLinks -MultiViews RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d 

Only go below if the requested file name is not a file or directory.

 RewriteRule .* index.php/$0 [PT,L] 

Rewrite any PATH path in index.php / PATH . Stop processing if it matches (pay attention to L as last ). This means that nothing below will be activated.

 #RewriteRule brandlisting/(.*)/ site/brandlisting?id=$1 [L] #RewriteRule brandlisting/(.*) site/brandlisting?id=$1 [L] RewriteRule brandlisting/(.*)/ site/brandlisting?name=$1 [L] RewriteRule brandlisting/(.*) site/brandlisting?name=$1 [L] 

Assuming it doesn't match .* (Aka, never), check the path for other patterns. Also note that the two lines you commented on and the two lines you don't have match the same patterns. If someone worked, the other will not.

- Pro Tip: Regex has an abbreviated expression for including a rule with a trailing slash and without it: /? equivalently, either with one slash or without a slash. Another way to write this is: /{0,1} .

How to fix

.htaccess Redirection rules are a pain. My rule is to make them as easy to write as possible, which makes them easier to read and maintain. How to do it? Direct redirection logic to your PHP program, rather than force Apache to rely on it.

Solution 1

The htaccess approach is only for you to understand that Apache rewrite flags tell your server and configure accordingly. You can do this here in one of two ways:

  • First create your more specific rules. those. move the rules /site/brandlisting?name=$1 to the rules .* .

  • Add separate rewrite conditions for any subsequent processing. According to the Apache2 documentation above:

The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, further rules will not be processed. This corresponds to the last command in Perl or the break command in C. Use this flag to indicate that the current rule should be applied immediately, without considering further rules.

The key point here is that it is inside each set of rules . The construction of a new set of rules will continue.

An example that should work (not verified):

 RewriteEngine on RewriteCond %{HTTP_HOST} ^localhost/mate/admin$ [NC,OR] RewriteCond %{HTTP_HOST} ^localhost/mate/admin$ RewriteCond %{REQUEST_URI} !wordpress/ RewriteRule (.*) /wordpress/$1 [L] Options +FollowSymLinks -MultiViews RewriteEngine On RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$0 [PT,L] #New Rule Set RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Note that /? is the same as writing two separate rules, # one with a slash at the end, and one without. RewriteRule brandlisting/(.*)/? site/brandlisting?name=$1 [L] 

There are some great built-in routers like Silex and Slim . If you do not want to use them, you can use your own internal logic, which analyzes various fragments. In my experience, the more I can break out of my .htaccess file, the happier you will be. I am sure that it is much easier to debug problems and easier to sort through changes without introducing unforeseen consequences.

Decision 2

You can use PHP routers in combination with the above .htaccess file, for example:

 // Slim example $app = new Slim\Slim(); $app->get('/brandlisting/:key', function ($key) { if (is_numeric($key)) { $id = $key; // call/run $id logic } else { $name = $key; // call/run $name logic } }); // ... $app->run(); 

If you do, you can remove any of your logical brandlisting with .htaccess and put it in your index.php instead.

Conclusion

If you can help, experience tells me that it’s better not to write application logic in .htaccess rules. Instead, make your .htaccess rules simple and use a routing library. If you want to use .htaccess , make sure that you understand the Apache rewrite flags that you use, and that Apache reads everything from the top level bottom. For example, if the [L] flag is used, Apache stops reading all other rules in the ruleset . This means that you need to create a new set of rules with additional rules that need to be processed, or to introduce more specific rules first. Keep in mind that if these rules have the [L] flag, they will also stop the execution of any subsequent rules.

+1
source share

I start with your current .htaccess , you have a crunch of the whole available rule that meets your needs for a single .htaccess , for example, for wordpress, which should be in the directory where your Wordpress is installed.

Your rule according to your requirement should be like this if you try in the root directory,

 RewriteEngine on RewriteBase /mate/admin/ RewriteRule site/brandlisting/([\d]+)/?$ site/brandlisting.php?id=$1 [L] RewriteRule site/brandlisting/([a-zA-Z]+)/?$ site/brandlisting.php?name=$1 [L] 

And for the wordpress directory, you can create a separate .htaccess where you can put your index.php rule.

+2
source share

Here you can use redirection.

Assuming you have the following folders:

 <server root>/mate/admin 

host in .htaccess in mate / admin

 RewriteBase /mate/admin/ RewriteRule "site/brandlisting/([^\\]+)/" "site/brandlisting?id=$1" [R,L] RewriteRule "site/brandlisting/([^\\]+)" "site/brandlisting?id=$1" [R,L] 
+1
source share

You may have a problem when a Directory or other directive causes rewriting rules to re-trigger and cause endless redirects.

See the documentation for RewriteRule Flags: L.

In particular, the following cited text applies:

If you use the RewriteRule in .htaccess files or in sections, it is important to have some idea of ​​how the rules are processed. A simplified form of this is that once the rules have been processed, the rewritten request is passed back to the URL parser to do what it can do with it. It is possible that when the processed request is processed, the .htaccess file or section may be encountered again, and thus the ruleset may be run again from the very beginning. Most often this will happen if one of the rules causes a redirection - both internal and external - causes the process to be requested to start.

Therefore, it is important if you use the RewriteRule directives in one of these contexts, you take specific measures to avoid the rules and not rely solely on the [L] flag to stop execution from a number of rules, as shown below.

You may need to add a rewrite condition or perform other steps to prevent a loop.

0
source share

All Articles