.htaccess不需要地重定向

时间:2014-01-28 09:55:10

标签: apache .htaccess mod-rewrite redirect

我希望在调用domain.ext/sitemap.xml时,会打开domain.ext/sitemap.xml.php我开始工作了。

我的问题是浏览器重定向到php文件,我不想要它,它应该继续说sitemap.xml

# Catch sitemap
RewriteCond %{REQUEST_URI} ^/sitemap.xml$
#RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^(.*) http://%{HTTP_HOST}/sitemap.xml.php [L]


## Internally rewrite extensionless file requests to .php files ## 
# If the requested URI does not contain a period in the final path-part 
RewriteCond %{REQUEST_URI} !(\.[^./]+)$ 
# test if sitemap
RewriteCond %{REQUEST_URI} !^/sitemap 
# and if it does not exist as a directory 
RewriteCond %{REQUEST_FILENAME} !-d 
# and if it does not exist as a file 
RewriteCond %{REQUEST_FILENAME} !-f 
# then add .php to get the actual filename 
RewriteRule ^(.*)/? index.php?q=$1 [L]

任何能告诉我原因的人?

1 个答案:

答案 0 :(得分:1)

从目标URI中删除http://以启用内部重写:

# Catch sitemap
RewriteRule ^sitemap\.xml$ /sitemap.xml.php [L]

## Internally rewrite extensionless file requests to .php files ## 
# If the requested URI does not contain a period in the final path-part 
RewriteCond %{REQUEST_URI} !(\.[^./]+)$ 
# test if sitemap
RewriteCond %{REQUEST_URI} !^/sitemap 
# and if it does not exist as a directory 
RewriteCond %{REQUEST_FILENAME} !-d 
# and if it does not exist as a file 
RewriteCond %{REQUEST_FILENAME} !-f 
# then add .php to get the actual filename 
RewriteRule ^(.+?)/?$ index.php?q=$1 [L]

PS:如果目标URI以http://开头,则mod_rewrite重定向为R=302状态。