htaccess重写规则到不同的URL

时间:2015-02-24 07:38:18

标签: apache .htaccess mod-rewrite

好的,我在尝试指向和重定向两种不同类型的链接时遇到了问题。我有:

www.example.com/listings.php?packageType=Sample&packageTypeSub=Sample2

当链接通过地址栏时更改为:

www.example.com/Sample/Sample2

但是,当我有另一页

www.example.com/location.php?place=sampleplace

点击此链接可转到

www.example.com/sampleplace

哪个没问题,但它使用的是listing.php页面,而不是location.php。 这是我的htaccess:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /


#####################################################
# Rewrite specific pages - listings
# Allow packageType
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]   
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+) [NC]
RewriteCond %{QUERY_STRING} !&packageTypeSub=([^&]+)
RewriteRule ^ /%1/? [R=301,NC]
RewriteCond %{REQUEST_URI}  !(upload-images) [NC]
RewriteRule ^([^/]+)/([^/]+)?$ /listings.php?packageType=$1 [L]

# Allow packageTypeSub
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]   
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+)&packageTypeSub=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,NC]
RewriteCond %{REQUEST_URI}  !(upload-images) [NC]
RewriteRule ^([^/]+)/([^/]+)?$ /listings.php?packageType=$1&packageTypeSub=$2 [L]


#####################################################
# Rewrite specific pages - location
RewriteCond %{THE_REQUEST} \ /+location\.php\? [NC]   
RewriteCond %{QUERY_STRING} (?:^|&)place=([^&]+) [NC]
RewriteRule ^ /%1/? [R=301,NC]
RewriteCond %{REQUEST_URI}  !(upload-images) [NC]
RewriteRule ^([^/]+)/([^/]+)?$ /location.php?place=$1 [L]


#####################################################
# All other pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)$ $1.php [L]

包含' upload-images'已被添加以忽略此文件夹,因为我遇到了正确显示图像的问题。

看起来这个位置正在通过列表并且没有到达位置。我做错了什么?

1 个答案:

答案 0 :(得分:0)

试试这个.htaccess:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

#####################################################
# Rewrite specific pages - location
RewriteCond %{THE_REQUEST} \ /+location\.php\? [NC]   
RewriteCond %{QUERY_STRING} (?:^|&)place=([^&]+) [NC]
RewriteRule ^ location/%1/? [R=301,L]

RewriteCond %{REQUEST_URI}  !(upload-images) [NC]
RewriteRule ^location/([^/]+)/([^/]+)?$ location.php?place=$1 [L,QSA]

#####################################################
# Rewrite specific pages - listings
# Allow packageType
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]   
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+) [NC]
RewriteCond %{QUERY_STRING} !&packageTypeSub=([^&]+)
RewriteRule ^ %1/? [R=301,L]

RewriteCond %{REQUEST_URI}  !(upload-images) [NC]
RewriteRule ^([^/]+)/([^/]+)?$ listings.php?packageType=$1 [L,QSA]

# Allow packageTypeSub
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]   
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+)&packageTypeSub=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R=301,L]

RewriteCond %{REQUEST_URI} !(upload-images) [NC]
RewriteRule ^([^/]+)/([^/]+)?$ listings.php?packageType=$1&packageTypeSub=$2 [L,QSA]    

#####################################################
# All other pages
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^/.]+)$ $1.php [L]