mod rewrite everything after domain into get

2019-03-04 02:34发布

问题:

How can I rewrite everything after the domain name into get if it is not already get?

For example : example.com/blah/blah.blah

will become example.com/?blah/blah.blah

basically all I want to do is add a ? after the first forward slash if there isnt one already.

Thanks!

回答1:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /index.php?$0 [QSA]

But this solution has some issues, @anubhava's is better ;-)



回答2:

Quite simply use variable %{REQUEST_URI} as a query parameter:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule . /index.php?%{REQUEST_URI} [L,QSA]


回答3:

Another technique is to use the $_GLOBALS['PATH_INFO'] variable which will give you the rest of the path after the script name, so for:

http://example.com/somefile.php/james/fred/blogs.csv?something=value

PATH_INFO would be set to "/james/fred/blogs.csv" and you would still have the possibility of using GET/POST variables separately as modifiers. This can be quite useful, for example if you want to create a .csv file and have it appear to the remote browser client as if it were named "blogs.csv".