mod_rewrite指令可以自己工作,但不能在具有concrete5的完整htaccess文件的上下文中工作

时间:2013-10-09 23:02:06

标签: apache .htaccess mod-rewrite concrete5

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

我正在尝试重写格式为

的网址
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/被重定向到http://www.mywebsite.com/index.php的网址

有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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的那条规则下面。