重写规则不按预期工作?

时间:2013-08-09 09:28:38

标签: .htaccess

我有一个带有参数的网址,我希望将其制作成sef网址:

想:

http://map.tautktiv.com/street.php?address=abc

成为:

http://map.tautktiv.com/street/address/abc

http://map.tautktiv.com/address/abc

已经尝试了几个在线工具来生成.htaccess规则,但它们都没有对URL产生任何影响,.htaccess文件处于活动状态(试图在其中加入一些乱码并获得错误500)

这些是我试过的规则:

1

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^address-([^-]*)$ /street.php?address=$1 [L]
RewriteRule street/address/(.*) street.php?address=$1

2

Options +FollowSymLinks
RewriteEngine on

RewriteRule /address/(.*)\.php street.php?address=$1

3

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add whatever other special conditions you need here

RewriteRule ^/([0-9]+)-(.*)$ /street.php?address=$1 [L]
RewriteRule /(.*)/(.*)/$ street.php?address=$1

该站点是一个子域,文件驻留在共享主机GoDaddy服务器的子目录中,也试图将这些规则应用到它上面的目录中的.htaccess,结果相同。

还尝试了以下建议

Options +FollowSymLinks
RewriteEngine on 

RewriteBase /

RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]

同样的结果,没有任何反应。

尝试直接从主域转到页面但结果相同:

http://tautktiv.com/map/streets/street.php?address=abc

2 个答案:

答案 0 :(得分:1)

第一条规则会将丑陋的网址重定向到漂亮的网址。 第二条规则将在内部将其重定向,以便用户不会看到丑陋的URL。

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# Internally forward /street/address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^street/address/(.*)/?$ /street.php?address=$1 [NC,L]

# Internally forward /address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^address/(.*)/?$ /street.php?address=$1 [NC,L]

如果您确认规则按预期工作,则可以将其从302更改为301,因为在您知道规则按预期工作之前,您不想使用301。

.htaccess应该放在street.php所在的文件夹中。

HTTP是US ASCII,因此您的语言会失败,它会将其重定向到以下内容:

  

/街道/地址/%25D7%2590%2520%25D7%2598%25D7%2591%25D7%25A8%25D7%2599%2520%25D7%2599%25D7%25A8%25D7%2595%25D7%25A9%25D7 %259C%25D7%2599%25D7%259D%2520%25D7%2599%25D7%25A9%25D7%25A8%25D7%2590%25D7%259C

这里最好的选择是将链接更改为直接使用/street/address/word而不是php文件。

这样您就不需要第一条规则了,只能使用内部重定向,这对于此更新可以正常工作。

答案 1 :(得分:0)

试试这个:

RewriteEngine on 

RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]

在您的示例中,您在RewriteRule的第二行中错过了^$

使用[r=301,L]代替[L]告诉浏览器,thzis是premanent重定向。