开发者

Make htaccess put all directories into a single get variable

I am working with a CMS that needs pretty urls. I found this snippet of htaccess code that I thought would solve all my problems:

Options +FollowSymLinks  
RewriteEngine On  

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  

RewriteRule ^(\w+)$ ./index.php?route=$1  

Then on index.php I put this:

echo $_GET['route'];

If I go to mywebsite.com/cars, I can see "cars". Perfect. But if I go to mywebsite.com/cars/ford, I get a "page not found开发者_如何学运维". What am I doing wrong? I want everything after the first "/" to be stuck into the route variable so I can explode it and make magic.


This should work flawlessly:

RewriteEngine On  
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  
RewriteRule ^(.*)$ index.php [L]

You don't have to pass the URL as a GET parameter to your script, because that would cause additional headaches about escaping special characters. You can easily access your URL via $_SERVER['REQUEST_URI'].


Put these lines in your .htaccess file:

Options -MultiViews +FollowSymLinks
RewriteEngine On

RewriteRule ^(?!index\.php)(.*)$ /index.php?route=$1 [L,NC,QSA]


\w does not match slashes /. You need .+ instead of \w+:

RewriteRule ^(.+)$ ./index.php?route=$1  


First, try this as .htaccess:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ index.php/$1 [NC,L]

Get the query part (e.g. /a/b/c) like this:

$route = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

And explode it:

$arr = explode('/', $route)

The $arr[0] is the php script you want to run and $arr[1..n] is the query part.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜