1

我在使用一些我知道单独工作但不在完整 htaccess 文件的上下文中工作的 mod_rewrite 指令修改站点的 htaccess 文件时遇到问题。该站点的 CMS 是 Concrete5,因此在 htaccess 文件中有一些具体的 Concrete5 配置。

我正在尝试重写格式的 URL

http://www.mywebsite.com/proplist/?location=town&distance=3&proptype=buy&maxPrice=&minPrice=&bedrooms=&propertyType=

http://www.mywebsite.com/property/town/buy/

我有以下指令可以单独工作(我在另一个网络服务器的 webroot 下名为 proplist 的文件夹中创建了 index.php):

RewriteBase /proplist
RewriteRule property/([a-zA-z]+)/([a-zA-z]+)/$ http://www.mywebsite.com/property/\/?location=$1&distance=3&proptype=$2&maxPrice=&minPrice=&bedrooms=&propertyType= [R]
Redirect 301 /property/ http://www.mywebsite.com/proplist/

(我想我可以使用 %{REQUEST_FILENAME} 而不是http://www.mywebsite.com

我无法在已经存在的 htaccess 文件的上下文中使用上述内容(使用我的修正):

<IfModule mod_rewrite.c>
RewriteEngine On
#
# -- concrete5 urls start --
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
# -- concrete5 urls end --

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# -- rewrite property search urls --
RewriteBase /proplist
RewriteCond %{REQUEST_URI} property
RewriteRule property/([a-zA-z]+)/([a-zA-z]+)/$ http://www.mywebsite.com/proplist/\/?location=$1&distance=3&proptype=$2&maxPrice=&minPrice=&bedrooms=&propertyType= [R]
Redirect 301 /property/ %{REQUEST_FILENAME}/proplist/
RewriteBase /
# -- end rewrite property search urls --

#Gzip
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript
</ifmodule>
#End Gzip

# remove browser bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 1 week"
</IfModule>

## EXPIRES CACHING ##
AddType application/font-wof          .woff
AddType application/x-font-woff       .woff
AddType application/x-woff            .woff

# end of full htaccess file

http://www.mywebsite.com/property/woking/buy/上面的结果就是被重定向到的url等http://www.mywebsite.com/index.php

任何人都可以帮忙吗?

4

1 回答 1

0

您需要做的第一件事是将所有属性搜索 URL 规则放在具体的 5 个路由规则之前。路由规则完全破坏了 URI,当发生重定向时,被破坏的 URI 最终会被重定向。

这意味着这些规则:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

必须您的 CMS 规则之前。

您可能还想从以下位置更改规则:

# -- rewrite property search urls --
RewriteBase /proplist
RewriteCond %{REQUEST_URI} property
RewriteRule property/([a-zA-z]+)/([a-zA-z]+)/$ http://www.mywebsite.com/proplist/\/?location=$1&distance=3&proptype=$2&maxPrice=&minPrice=&bedrooms=&propertyType= [R]
Redirect 301 /property/ %{REQUEST_FILENAME}/proplist/
RewriteBase /
# -- end rewrite property search urls --

至:

# -- rewrite property search urls --
RewriteRule ^property/([a-zA-z]+)/([a-zA-z]+)/$ /proplist/?location=$1&distance=3&proptype=$2&maxPrice=&minPrice=&bedrooms=&propertyType= [L]
RewriteRule ^property/$ /proplist/ [L]
# -- end rewrite property search urls --

但我不太确定你原来的规则有什么意义。它们是 mod_alias 和 mod_rewrite 的混合体,这肯定是一团糟。如果您实际上想要重定向而不是重写(这是您所要求的,重写不是重定向) ,请将标志从更改为[L]。因此,这两条规则需要位于重定向到.[L,R=301]www

于 2013-10-09T23:22:24.150 回答