My mod_rewrite code in htaccess caused the page to not load

跟風遠走 提交于 2019-12-12 04:04:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!