问题
I have horrible URLs like this:
http://example.com/subfolder/index.php?a=1&b=1&token=8d519decbe60db44154b1fbc8c553ac4
I am trying to make it look like this:
http://example.com/subfolder/menu/1/1/#/
But this code is causing the page to crash/not be found or something:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^a=([0-9]+)&b=([0-9]+)&token=([A-Za-z0-9]+)
RewriteRule ^subfolder/index.php /menu/$1/$2/#? [R=301,L]
I put this .htaccess
file in my root folder example.com/.htaccess
, do I need to put it in the subfolder?
I'm getting 404 page not found error !
回答1:
Try a different approach:
RewriteEngine On
RewriteBase /subfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9]+)/([0-9]+)/([0-9]+)/$ index.php?a=$1&b=$2&token=$3 [L,QSA]
Output: (http://example.com/subfolder/menu/1/1/#/)
<?php
print_r($_GET);
Array
(
[a] => menu
[b] => 1
[token] => 1
)
?>
Or http://example.com/subfolder/menu/1/18d519decbe60db44154b1fbc8c553ac4/#/
RewriteEngine On
RewriteBase /subfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9]+)/([0-9]+)/([A-Za-z0-9]+)/$ index.php?a=$1&b=$2&token=$3 [L,QSA]
UPDATE:
RewriteEngine On
RewriteBase /subfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9]+)/([0-9]+)/([0-9]+)/([A-Za-z0-9]+)/$ index.php?a=$2&b=$3&token=$4 [L,QSA]
Will match : mysite.com/subfolder/menu/1/2/blah123
and pass to index.php?a=1&b=2&token=blah123
UPDATE 2 (as there is not subfolder - see comment):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([0-9]+)/([0-9]+)/([A-Za-z0-9]+)/$ index.php?a=$3&b=$4&token=$5 [L,QSA]
Will match : mysite.com/subfolder/menu/1/2/blah123
and pass to index.php?a=1&b=2&token=blah123
Please have a read of this site: http://corz.org/serv/tricks/htaccess2.php
回答2:
Don't you want to include 'subfolder' in the rewritten url?
RewriteRule ^subfolder/index.php /subfolder/menu/$1/$2/#? [R=301,L]
来源:https://stackoverflow.com/questions/10368980/my-mod-rewrite-code-in-htaccess-caused-the-page-to-not-load