简体   繁体   中英

URL redirects in htaccess

I have a htaccess file that is set and running on a live server. Everything is functioning fine with it but I have to add some specific URL re-directs to it. The specific re-directs had been setup in an IIS web.config file. An example:

<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>

I'm having a hard time translating this to mt htaccess file. I've tried:

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]

Obviously, the program_consumer is not a file that exists anywhere in the root document directory, but the site_programs does exist and is served as an extension-less PHP file. Any thoughts on what I'm missing?

Your regex in RewriteRule seems faulty and it needs to be placed before internal rewrite rule:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM