0

I have got this in the public_html/.htaccess:

RedirectMatch 301 ^/$ /site/

and a bunch of lines like:

RedirectMatch 301 ^/contact.php$ /site/page/contact

How can I change this to RewriteCond and RewriteRules to work the same but exclude anything inside public_html/test/ directory from these rewrites?

1 Answers1

0

I'd say this probably is what you want:

RewriteEngine on
RewriteCond
RewriteRule ^/?test/ "-" [L]
RewriteRule ^/?$ /site/ [R=301]
RewriteRule ^/?contact\.php$ /site/page/contact [R=301]

It will pass through all requests to /test, after that you can implement whatever rules you like.


And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • What does this part do: "-" [L] ? – StevenKong Aug 10 '17 at 20:11
  • @StevenKong As said: it passes through the matching requests and does _not_ modify them. – arkascha Aug 10 '17 at 20:12
  • By 'better in host configuration' do you mean httpd.conf file? Can you give an example how to do it there please? – StevenKong Aug 10 '17 at 20:12
  • @StevenKong Todays typical setups do not use a single configuration file, but a more modular approach with configuration files including others. The actualy structure depends on the setup you use, for example on the type of Linux distribution your setup is based on. About the example: the rules in the answer will work likewise in the real http servers host configuration and in dynamic configuration files. That is the reasons for the leading `^/?` by the way... – arkascha Aug 10 '17 at 20:15
  • I have looked up the [L] flag and it looks like it stops processing the rewrite rules for that request, but I am still not sure what "-" does. Can you please explain what "-" is for? – StevenKong Aug 11 '17 at 16:19
  • https://stackoverflow.com/questions/2102128/mod-rewrite-what-does-this-rewriterule-do/2102189#2102189 – arkascha Aug 11 '17 at 16:25
  • I suggest you start reading the official documentation of the tool you use. All that is explained in there. In this case that is Apache's rewriting module, so http://httpd.apache.org/docs/current/mod/mod_rewrite.html – arkascha Aug 11 '17 at 16:52