Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess url rewrite confusion about request_uri

Tags:

php

.htaccess

I'm (sometimes) somehow managing to url rewriting successfully but I want to understand the core of what I'm actually doing.

Now, whenever I type http://localhost/admin/shop/ to the browser's address bar, I'd like my htaccess to rewrite the url as http://localhost/admin.php?page=shop so in the php I can understand that I'm actually dealing with shop 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]

This works so far. But I'd like to understand why my code doesn't 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 basically, when you type 'http://localhost/admin/shop/' to the address bar, php's $_SERVER['REQUEST_URI'] will print out exactly /admin/shop/. Now, assuming php's REQUEST_URI is like that, htaccess REQUEST_URI should be the same right? I don't really know if they are using different engines but that's what comes logical to me. So, assuming that I'm right, why the second example doesn't work when I remove the '.php' from the RewriteCond and RewriteRule? Also, if I'd had the chance to print the REQUEST_URI of htaccess, what would it actually print to the screen in the above example?

PS: I know that for this case, I don't really need to use htaccess as I can create a folder inside admin folder and name it shop and so on. But the thing is that I don't really have an admin folder as I'm using controllers and a simple switch in the admin.php to avoid creating millions of folders inside my application. This is just simpler to me.

like image 532
Shaokan Avatar asked Nov 19 '11 13:11

Shaokan


3 Answers

§

The reason your amended rule doesn't work is probably because you omitted the .php from the url-path. Assuming you have an admin.php file hosted in the root this should work:

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

§

Removing this

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

probably didn't cause any problems as it probably wasn't matching anything. The pattern ends with escaped dollar sign \$ which matches the $ character and not the end of line meta-character. So that rule matches any path that contains a dollar sign.

§

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

More or less. But, for instance, if you did any rewriting with redirects you might get confused because the redirect will create a new, rewritten request. That's a specious example, but there could be other advanced use cases to watch out for. For simple cases, they are the same.

like image 131
Mark Fox Avatar answered Nov 13 '22 00:11

Mark Fox


Just a thought, i think the last slash doesn't let it match a .php file, ie: does http://localhost/admin/shop give you the same page as http://localhost/admin/shop/ ?

Try changing it to:

RewriteRule ^admin/(.*)$ admin\.php?pval=$1 [l,nc]
like image 42
lfxgroove Avatar answered Nov 13 '22 00:11

lfxgroove


If you are using a .htaccess file to do mod_rewrite goodness, REQUEST_URI doesn't have the beginning slash, so for example /admin/shop/ -> admin/shop/

And to "debug" mod_rewrite you have to setup the "RewriteLog" directive in your conf file, something like this:

RewriteLog "C:/wamp/logs/rewrite.log"
RewriteLogLevel 9
like image 1
no_name_here Avatar answered Nov 13 '22 00:11

no_name_here