.htaccess problem
I want to redirect what's in link after localhost/myScript/
to localhost/myScript/test.php?var=$1
.
So.. something like
localhost/myScript/this/is/just/a/test/
(with or without the last slash) would redirect to
localhost/myScript/test.php?var=t开发者_如何转开发his/is/just/a/test
This is what I've got, but it's not good enough.
RewriteRule ^(.+)$ test.php?var=$1
I've tried using
RewriteRule ^(.+)/?$ test.php?var=$1
But, I get $_GET['var'] = 'test.php'
.
I see 2 main approaches:
1. Two separate rules to deal with URL with and without trailing slash. It will only rewrite requests to non-existing files/folder thus preventing rewrite loop you are having:
# work with URL that ends with slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ test.php?var=$1 [L]
# work with the rest of URLs (that ends with no slash)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ test.php?var=$1 [L]
2. Have separate rule to ignore existing files (to not to rewrite already rewritten URLs) and then use 1 line rewrite rule for URL with and without trailing slash:
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# rewrite URLs
RewriteRule ^(.*[^/])/$ test.php?var=$1 [L]
RewriteRule ^(.+)$ test.php?var=$1 [L]
I would prefer #2.
this is better one
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.+?)$ test.php?var=$1 [L]
精彩评论