重写Mod仍然返回原始URL

时间:2015-11-25 17:29:54

标签: php regex apache .htaccess mod-rewrite

使用WamServer,我使用Generate It!覆盖了像

这样的URL

原始网址:

http://localhost/CMS/foo.php?id=2&slug=eyeglasses

重写的网址:

http://localhost/2/eyeglasses.php

网站给我的规则是

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /CMS/foo.php?id=$1&slug=$2 [L]

但这仍然是返回原始网址

http://localhost/CMS/foo.php?id=2&slug=eyeglasses

你能告诉我我做错了什么吗?

  

更新

更加清晰!我有index.php,其中包含

之类的链接
echo '<a href="foo.php?id='. $theID .'&slug='. $slug .'" class="list-group-item" data-id="'.$theID.'">'.$theName.'</a>';

现在我们的用户登陆foo.php,网址看起来像

http://localhost/CMS/foo.php?id=1&slug=eyeglassess

正确?我想做的是显示

http://localhost/2/eyeglasses.php

而不是原始网址

1 个答案:

答案 0 :(得分:1)

您需要一个重定向规则才能将旧网址重定向到漂亮网址。此代码应位于/CMS/.htaccess

RewriteEngine On
RewriteBase /CMS/

# redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /CMS/+foo\.php\?id=([^\s&]+)&slug=([^\s&]+) [NC]
RewriteRule ^ %1/%2.php? [R=302,L,NE]

RewriteRule ^([^/]+)/([^/]+)\.php$ foo.php?id=$1&slug=$2 [L,NC,QSA]

然而,最好将链接代码更改为新的漂亮链接方案。