I am trying to redirect http://example.com/* to http://example.com/ but everytime I end up with a redirect loop. What is a incorrect on this? None of these two (or any other combinations of regular expressions) works:

RedirectMatch temp ^/.+$ http://example.com
RedirectMatch temp ^/.+$ http://example.com/

This is always matched and a redirect loop is being created.


So you want anything not the absolute root on example.com to temporarily redirect to example.com. If so, I would suggest you try this:

RewriteEngine on
RewriteRule ^(.*)$ / [L,R=307]

Or perhaps this:

RewriteEngine on
RewriteRule ^.+$ / [R=307,NC,L]

EDIT: If you need to add a trailing slash to the URL, you can do this; using my second example above:

# Settings for adding a trailing slash to the URL
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]

RewriteEngine on
RewriteRule ^.+$ / [R=307,NC,L]

Also, this site has a nice/concise explanation of what the difference is between RedirectMatch (mod_alias) and RewriteRule (mod_rewrite).

  • That works, but I need to add a slash on the beginning, so it is RewriteRule ^/.+$ / [R=307,NC,L]. But I don't understand, why RedirectMatch is not working as expected.
    – Bruce
    Nov 13 '13 at 18:18
  • RedirectMatch uses mod_alias while RewriteRule` uses mod_rewrite. Since mod_alias just creates a simple alias, the server will loop since you are aliasing an alias to the same place you started. While mod_rewrite actually does regex matching to allow for subtleties as you describe. Will edit answer with more details. Nov 13 '13 at 18:27

RedirectMatch temp (example.com)/.*$ $1/


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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