2

I have 2 redirects that work fine:

Redirect 301 /namedeal/ca_domain_name.php /namedeal/cheap_domain_name.php
Redirect 301 /namedeal/ws_domain_name.php /namedeal/cheap_domain_name.php

I want to be able to replace them using regex so that any page with 2 letters followed by _domain_name.php will be redirected.

I have tried following but not working for me. Can anyone help me get this right?

RewriteRule ^/namedeal/([a-z][a-z])_domain_name\.php$ /namedeal/cheap_domain_name.php [R=301,L]

UPDATE:

Full content of .htaccess file which is in the sub directory namedeal:

IndexIgnore *

ErrorDocument 400 https://www.hostfast.com/namedeal/page_missing.php?code=400
ErrorDocument 401 https://www.hostfast.com/namedeal/page_missing.php?code=401
ErrorDocument 403 https://www.hostfast.com/namedeal/page_missing.php?code=403
ErrorDocument 404 https://www.hostfast.com/namedeal/page_missing.php?code=404
ErrorDocument 500 https://www.hostfast.com/namedeal/page_missing.php?code=500

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

#RewriteRule ^namedeal/[a-z]{2}_domain_name\.php$ /namedeal/cheap_domain_name.php [R=301,L]

Redirect 301 /namedeal/ca_domain_name.php /namedeal/cheap_domain_name.php
Redirect 301 /namedeal/ws_domain_name.php /namedeal/cheap_domain_name.php
Redirect 301 /namedeal/dk_domain_name.php /namedeal/cheap_domain_name.php
Redirect 301 /namedeal/bz_domain_name.php /namedeal/cheap_domain_name.php
Redirect 301 /namedeal/cn_domain_name.php /namedeal/cheap_domain_name.php
Redirect 301 /namedeal/asia_domain_name.php /namedeal/cheap_domain_name.php

#set long expiry dates for faster page loading
ExpiresActive On
ExpiresDefault A3
# Set up caching on media files for 1 month
<FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf|swf|mov|mp3|wmv|ppt)$">
  ExpiresDefault A2419200
  Header append Cache-Control "public"
</FilesMatch>

# Set up 2 Hour caching on commonly updated files
<FilesMatch "\.(xml|txt|html|js)$">
  ExpiresDefault A7200
  Header append Cache-Control "private, must-revalidate"
</FilesMatch>

# Force no caching for dynamic files
<FilesMatch "\.(php|cgi|pl|htm|css)$">
  ExpiresDefault A0
  Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
  Header set Pragma "no-cache"
</FilesMatch>

# Turn off Etags to speed up page loading
Header unset ETag
FileETag None
<Files 403.shtml>
order allow,deny
allow from all
</Files>

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  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
</IfModule>

Ok still not working for me.

RewriteRule ^namedeal/[a-z]{2}_domain_name\.php$ /namedeal/cheap_domain_name.php [R=301,L]

#Redirect 301 /namedeal/ca_domain_name.php /namedeal/cheap_domain_name.php

The commented out line below does work.

The one with regex does not

1 Answer 1

2
RewriteRule ^/namedeal/([a-z][a-z])_domain_name\.php$ /namedeal/cheap_domain_name.php [R=301,L]

In .htaccess the URL-path matched by the RewriteRule pattern does not start with a slash, so this should be:

^namedeal/[a-z][a-z]_domain_name\.php$

No need for the capturing subpattern (ie. remove the parentheses).


Aside: If _domain_name.php is the same in both the URL being matched and the substitution string then you can avoid repetition by capturing this and use a backreference. For example:

 RewriteRule ^namedeal/[a-z]{2}(_domain_name\.php)$ /namedeal/cheap$1 [R=301,L]

The $1 backreference contains _domain_name.php matched in the RewriteRule pattern.

Thew {2} quantifier matches the preceding pattern exactly twice, ie. the same as [a-z][a-z].


UPDATE#1: This htaccess is in the sub directory /namedeal/

Ah, that's the other "problem". The URL-path that the RewriteRule pattern matches against is relative to the current directory. If the .htaccess file is inside the /namedeal/ directory then the URL-path matched by RewriteRule does not contain the /namedeal/ directory.

The directive should be written like this instead:

RewriteRule ^[a-z]{2}_domain_name\.php$ /namedeal/cheap_domain_name.php [R=301,L]

UPDATE#2: Since you have no other mod_rewrite directives in your .htaccess file (Redirect is a mod_alias directive) then you should really be using a mod_alias RedirectMatch directive instead of a mod_rewrite RewriteRule here.

It is advisable to avoid mixing redirects from both modules as you can get unexpected conflicts. Different modules execute independently, which is not necessarily in the order they appear in the config file. (In this example it doesn't really matter - mod_rewrite executes first and you have the mod_rewrite directive first anyway.)

RedirectMatch follows the same format as Redirect and matches against the full URL-path, starting with a slash prefix. So this would be:

RedirectMatch 301 ^/namedeal/[a-z]{2}_domain_name\.php$ /namedeal/cheap_domain_name.php

(Ironically, if you'd used RedirectMatch in the first place then your regex would have been correct.)

7
  • Thank you. Working on this now.
    – kmm2908
    Commented Apr 29, 2020 at 9:17
  • Ok still not working for me. RewriteRule ^namedeal/[a-z]{2}_domain_name\.php$ /namedeal/cheap_domain_name.php [R=301,L] #Redirect 301 /namedeal/ca_domain_name.php /namedeal/cheap_domain_name.php The commented out line below does work. The one with regex does not. Even if I point the regex version to redirect to the index page it still doesn't work. It just isn't detecting the url using the regex. Can anyone explain why? Thanks in advance again.
    – kmm2908
    Commented Apr 29, 2020 at 13:01
  • Assuming you have a RewriteEngine On directive, you may have a conflict with existing directives. Do you have other directives in your .htaccess file? Do you have any other .htaccess files in subdirectories? Please edit your question to include the contents of your .htaccess file.
    – MrWhite
    Commented Apr 29, 2020 at 13:38
  • Also, I'm assuming this .htaccess file is in the document root of your site?
    – MrWhite
    Commented Apr 29, 2020 at 13:50
  • 1
    Redirect match did it for me! Many thanks!
    – kmm2908
    Commented Apr 30, 2020 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.