URL重定向在htaccess中

时间:2014-11-04 20:09:51

标签: php apache .htaccess mod-rewrite redirect

我有一个在实时服务器上设置并运行的htaccess文件。一切都运行良好,但我必须添加一些特定的URL重定向到它。已在IIS web.config文件中设置了特定的重定向。一个例子:

<rule name="URL re-direct" stopProcessing="true">
     <match url="^program_consumer((\.cfm)|(\.php)|()+)((\?.*)|())$" />
     <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
     <action type="Redirect" url="site_programs{R:7}" />
 </rule>

我很难将其翻译成mt htaccess文件。我试过了:

AddDefaultCharset OFF
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

# Re-direct Non-file Non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([program_consumer]+)((\.cfm)|(\.php)|()+)((\?.*)|())$ http://{HTTP_HOST}/site_programs$1 [L,R=301]

显然,program_consumer不是存在于根文档目录中任何位置的文件,但是site_programs确实存在并且作为无扩展名的PHP文件提供。对我缺少什么的想法?

1 个答案:

答案 0 :(得分:0)

RewriteRule中的正则表达式似乎有问题,需要在内部重写规则之前放置:

AddDefaultCharset OFF
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^(program_consumer\.(?:cfm|php))$ /site_programs/$1 [L,R=301,NC,NE]

# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]