Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mod_rewrite help with the regex

Status
Not open for further replies.

flycast

Programmer
Mar 14, 2007
8
US
I have a mod rewrite that I need to do. What I want to do is redirect the entire site to the root index.php file. I have tried a few things that are totally screwed up. I believe that everything I try (like (.*) /index.php just throws the server into an infinite loop.

I need something like:

(redirect everything) unless its (index.php)

How would I pull this off?
 
Something like this should work. Add this to your VirtualHost.
Code:
RewriteEngine On
RewriteLogLevel 0
RewriteCond %{REQUEST_URI} index.php$
RewriteRule !^/index.php [URL unfurl="true"]http://new.domain.com[/URL] [R,L]

M. Brooks
 
Awesome! Thank for the help! I did have to change one thing, on the second line I added the ! to signal "not" index.php.

RewriteEngine On
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule !^/index.php /index.php

By the way, what does the [R,L] do?
 
Hi

flycast said:
By the way, what does the [R,L] do?
httpd.apache.org said:
* '[tt]redirect|R[/tt] [=code]' (force redirect)
Prefix Substitution with [tt][:thisport]/[/tt] (which makes the new URL a URI) to force a external redirection. If no code is given, a HTTP response of 302 (MOVED TEMPORARILY) will be returned. If you want to use other response codes in the range 300-400, simply specify the appropriate number or use one of the following symbolic names: [tt]temp[/tt] (default), [tt]permanent[/tt], [tt]seeother[/tt]. Use this for rules to canonicalize the URL and return it to the client - to translate ``[tt]/~[/tt]'' into ``[tt]/u/[/tt]'', or to always append a slash to [tt]/u/[/tt]user, etc.
Note: When you use this flag, make sure that the substitution field is a valid URL! Otherwise, you will be redirecting to an invalid location. Remember that this flag on its own will only prepend [:thisport]/ to the URL, and rewriting will continue. Usually, you will want to stop rewriting at this point, and redirect immediately. To stop rewriting, you should add the 'L' flag.

* '[tt]last|L[/tt]' (last rule)
Stop the rewriting process here and don't apply any more rewrite rules. This corresponds to the Perl [tt]last[/tt] command or the [tt]break[/tt] command in C. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules. For example, use it to rewrite the root-path URL ('[tt]/[/tt]') to a real one, e.g., '[tt]/e/www/[/tt]'.

Feherke.
 
Would this be the correct way to exclude both index.php or missing.php?

RewriteCond %{REQUEST_URI} !index.php$|!missing.php
RewriteRule !^/index.php|!^missing.php /index.php

I have tried that and it seems to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top