Htaccess url rewrite confusion about request_uri

I (sometimes) somehow know how to correctly rewrite the URL, but I want to understand the essence of what I'm actually doing.

Now when I type http://localhost/admin/shop/ into the address bar of the browser, I would like htaccess to rewrite the URL as http://localhost/admin.php?page=shop , so in php I can understand that I’m actually dealing with a store page. For this I have:

 RewriteEngine on RewriteRule ^(.*)\$ $1.php [nc] RewriteCond %{REQUEST_URI} ^/admin\.php/[^/]+/$ RewriteRule ^admin\.php/(.*)/$ admin\.php?pval=$1 [l,nc] 

It still works. But I would like to understand why my code does not work when I do this:

 ###RewriteEngine on ###RewriteRule ^(.*)\$ $1.php [nc] ### note the commenting out the code above RewriteCond %{REQUEST_URI} ^/admin/[^/]+/$ RewriteRule ^admin/(.*)/$ admin?pval=$1 [l,nc] ### note the removal of '\.php' 

So, when you type "http: // localhost / admin / shop /" in the address bar, php $_SERVER['REQUEST_URI'] will print exactly / admin / shop /. Now, assuming php REQUEST_URI like this, htaccess REQUEST_URI should be the same right? I really don't know if they use different engines, but that is logical for me. So, assuming I'm right, why does the second example not work when I remove the '.php' from the RewriteCond and RewriteRule ? Also, if I had the opportunity to print REQUEST_URI htaccess, what would it actually print on the screen in the above example?

PS: I know that for this case I really do not need to use htaccess, since I can create a folder inside the admin folder and call it a store and so on. But the fact is, I don’t actually have an admin folder, since I use controllers and a simple switch in admin.php to avoid creating millions of folders inside my application. For me it's just easier.

+6
source share
3 answers

Β§

The reason your corrected rule doesn't work is probably because you skipped .php from the url. Assuming you have an admin.php file located in the root, this should work:

 RewriteCond %{REQUEST_URI} ^/admin/[^/]+/$ RewriteRule ^admin/(.*)/$ /admin.php?pval=$1 [L,NC] 

Β§

Delete this

 RewriteRule ^(.*)\$ $1.php [nc] 

probably did not cause any problems, since it probably did not match anything. The sample ends with an escaped dollar sign \$ , which matches the $ character, not the end of line meta tag. Thus, this rule matches any path that contains a dollar sign.

Β§

Is Apache %{REQUEST_URI} the same variable as PHP $_SERVER['REQUEST_URI'] ?

More or less. But, for example, if you were rewriting with forwarding, you might get confused because the redirect will create a new, rewritten request. This is a good example, but other advanced use cases could be used for this. For simple cases, they are the same.

+5
source

Just a thought, I think that the last slash does not allow it to correspond to a .php file, i.e.: http://localhost/admin/shop provides you the same page as http://localhost/admin/shop/ ?

Try changing it to:

 RewriteRule ^admin/(.*)$ admin\.php?pval=$1 [l,nc] 
+2
source

If you use the .htaccess file to enhance mod_rewrite, REQUEST_URI does not have a slash, for example, / admin / shop / β†’ admin / shop /

And for "debugging" mod_rewrite you need to set up the "RewriteLog" directive in your conf file, something like this:

 RewriteLog "C:/wamp/logs/rewrite.log" RewriteLogLevel 9 
+1
source

Source: https://habr.com/ru/post/901890/


All Articles