使用htaccess重定向和重写多个URL

时间:2015-06-10 17:48:57

标签: php apache .htaccess mod-rewrite redirect

我正在尝试重定向并重写以下网址,但在某些情况下,我想保留一些值,但隐藏在网址中。以下是我想要的例子:

我有这个网址:

  • www.mysite.com/?a=blue
  • www.mysite.com/?a=red
  • www.mysite.com/?a=orange&b=omega&c=echo&d=zulu
  • www.mysite.com/?a=orange&b=alfa&c=charlie&d=delta

我想得到这个网址:

  • www.mysite.com/blue
  • www.mysite.com/red
  • www.mysite.com/orange/zulu
  • www.mysite.com/orange/delta

我有这个代码,前两个例子效果很好但是对于带有橙色的例子不起作用:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} /\?a=orange&b=([^\s&]+)&c=([^\s&]+)&d=([^\s&]+) [NC]
RewriteRule ^ /orange/%3/? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/orange/([^/.]+)/?$ ?a=orange&b=%1&c=%2&d=([^\s&]+) [L,QSA]

RewriteCond %{THE_REQUEST} /\?a=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ ?a=$1 [L,QSA]
</IfModule>

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/\?a=([^&]+)&b=([^&]+)&c=([^&]+)&d=([^&\s]+) [NC]
RewriteRule ^ /%1/%4? [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/(.*?)/?$ /?a=$1&d=$2 [QSA,NC,L]

答案 1 :(得分:0)

我已经解决了迁移我的&#34; spaghetti代码的问题&#34;到Laravel框架,并使用文件routes.php传递变量和刀片模板,以尽量减少这些变量的使用。