漂亮的URL内部重定向到丑陋的URL

时间:2017-11-17 11:12:52

标签: php regex apache .htaccess mod-rewrite

请参阅该链接:how to configure apache for xampp on mac osx lion to use mod_rewrite 我的问题:

这对我帮助很大,但是如果链接看起来像这样:http://localhost/category.php?category=something&id=2

如何将其更改为:http://localhost/something/2并且第二条规则将重定向重定向回来。

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index.php\?([^&=]+)=([^&\ ]+)&([^&=]+)=([^&\ ]+)(\ |$) [NC] RewriteRule ^ /%2/%4/? [R=302,L].

我认为就是这样。

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

无效。我必须重写^(。+)/?$,但我不知道如何。

我的代码:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# Redirect /category.php?category=something to /something

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?([^&=]+)=([^&\ ]+)
(\ |$) [NC]
RewriteRule ^ /%2? [R=302,L]

# Internally forward /something to /category.php?category=something
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ /index.php?page=$1 [QSA,NC,L]

# Redirect /category.php?category=something&id=ID to /something/ID
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?([^&=]+)=([^&\ 
]+)&([^&=]+)=([^&\ ]+)(\ |$) [NC]
RewriteRule ^ /%2\/%4/?  [R=302,L]

# Internally forward /something/ID to /category.php?category=something&id=ID
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ /index.php?page=$1 [QSA,NC,L]

0 个答案:

没有答案