Forum Moderators: phranque

Message Too Old, No Replies

What RewriteCond to use

         

chy123

4:21 pm on Jul 1, 2009 (gmt 0)

10+ Year Member



I want to set up a redirect so that

[domain...] goes to [domain...]
and
[domain...] goes to [domain...]

I don't want it to start rewriting my static html and jpg files so included some RewriteCond rules

RewriteCond %{REQUEST_URI} !(.*).htm(.*)
RewriteCond %{REQUEST_URI} !(.*).jp(.*)
RewriteRule ^/(.*)$ /folder1/folder2/$1 [P,NE]

The rule above works ok, only problem is that I have the homepage of my website on [domain...]

and that one gets redirected to [domain...] now with that rule in place.

I have my website on [domain...]

DirectoryIndex index.html
but I've set it up that it also works on just the domain main

What RewriteCond can I use to check that the current url isn't just the homepage on htpp://domain/ ?

Any suggesions ? Thanks

jdMorgan

5:54 pm on Jul 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like a much-simpler single-line rule will suffice here:

# Internally rewrite non-blank URL paths not containing slashes or periods to /folder1/folder2 subdirectory.
RewriteRule ^([^./]+)$ /folder1/folder2/$1 [L]

An intrinsic feature of this approach is that it prevents an 'infinite' rewriting loop, because the "/folder1/folder2" path contains slashes. So, the pattern itself prevents recursion and avoids "/folder1/folder2/peter" getting re-rewritten to "/folder1/folder2/folder1/folder2/peter" and then getting rewritten again to "/folder1/folder2/folder1/folder2/folder1/folder2/peter", etc.

BTW, avoid the use of ".*" subpatterns -- and especially multiple ".*" subpatterns whenever possible. While ".*" is an 'easy' regex pattern to understand, it is seldom needed, and often leads to unexpected results because it matches "anything, everything, or nothing."

Use of multiple ".*" subpatterns can also be horribly inefficient, because it forces the matching engine to do multiple back-off-and-retry matching attempts --dozens, hundreds, or even thousands of them in some cases-- trying to find a 'best fit' of the match-string to the pattern.

As a result, it is best to make your patterns as specific as possible, and to use all of the 'tricks' available in regular expressions to match *exactly* what you want to match, and as efficiently as possible. See the regular-expressions tutorial cited in out Apache Forum Charter for assistance with this subject.

Jim

chy123

9:21 pm on Jul 1, 2009 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for your suggestion. I tried to apply that one line rule : RewriteRule ^([^./]+)$ /folder1/folder2/$1 [L]

After restarting apache I was still able to access

[domain...]
and
[domain...]

[domain...] ALSO WORKED !

But the url

[domain...] didn't go to [domain...] (and I checked, that file does exist, doesn't have an extension though)

Do you know why ? Thanks

jdMorgan

9:57 pm on Jul 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Completely flush your browser cache (delete IE Temporary Internet Files), disable MultiViews if you're not using content-negotiation, and try again.

Separate problem: All *files* on your server should have filetype, so that the server will know what MIME-type it should indicate when sending the HTTP Content-Type header back to the client. So, the physical file should probably be named "test.html" and the rule should be changed to add that ".html" to the URL-path, as well as adding the subdirectory path "/folder1/folder2" as it does right now.

[added] I missed an important clue in your first post: If you are putting this code into a server config file, and not into .htaccess, and the code is *not* going into a <Directory> container in that server config file, then the code will need to be tweaked:


# Internally rewrite non-blank URL paths not containing slashes or periods to /folder1/folder2 subdirectory.
RewriteRule [b]^/([[/b]^./]+)$ /folder1/folder2/$1 [L]

[/added]

Jim

[edited by: jdMorgan at 10:02 pm (utc) on July 1, 2009]

chy123

10:43 pm on Jul 1, 2009 (gmt 0)

10+ Year Member



Hello jim, thanks for your quick reply. yes, I had put the rewriterule in the httpd.conf file not .htaccess file. Didn't know it would any difference.

I had a look at my apache httpd.conf and for the root all I have is

<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>

I just noticed that I have the domain in a virtual host:

<VirtualHost 192.168.1.100>
DocumentRoot /var/www/website

and there is NO <Directory "/var/www/website"> declaration. would it use the / one ? ---> <Directory />

Thanks again

chy123

10:45 pm on Jul 1, 2009 (gmt 0)

10+ Year Member



Forgot to mention, I use Firefox to test the changes and after the httpd.conf change I ran "service httpd restart"

and then Ctrl + F5 in Firefox to make sure it's not a cached version. With Yslow / Net I can see that the page returns a HTTP Header Status 200 OK so that wouldn't be a cache page ?

chy123

11:13 pm on Jul 1, 2009 (gmt 0)

10+ Year Member



I almost forgot why the RewriteCond were used:

RewriteCond %{REQUEST_URI} !(.*).htm(.*)
RewriteCond %{REQUEST_URI} !(.*).jp(.*)
RewriteRule ^/(.*)$ /folder1/folder2/$1 [P,NE]

it's because in the future I could be using

[domain...] ---> [domain...]

g1smd

11:19 pm on Jul 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{REQUEST_URI} !(.*).htm(.*) 
RewriteCond %{REQUEST_URI} !(.*).jp(.*)

Multiple .* patterns are always inefficient, however in this case, since you are not re-using the backreference data elsewhere, they can be completely omitted.

RewriteCond %{REQUEST_URI} !\.htm$
RewriteCond %{REQUEST_URI} !\.jpg$

Be aware these match more than just .htm and .jp - you might want to end anchor these patterns (or you might not - I don't know all your requirements).

You must also escape all periods in patterns.

jdMorgan

1:04 am on Jul 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, your rule in httpd.conf, inside a <VirtualHost>, but not enclosed in a <Directory> section, becomes:

RewriteCond %{REQUEST_URI} !\.htm$
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteRule ^/([^/]+)$ /folder1/folder2/$1 [L]

-or-

RewriteCond %{REQUEST_URI} !\.htm$
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteCond %{REQUEST_URI} !^/folder1/folder2/
RewriteRule ^/(.+)$ /folder1/folder2/$1 [L]

Either will work if there are to be no "subdirectories" under /folder1/folder2/
Use the second one if you do plan to have subdirectories under that subfolder, and do not plan to have any subdirectories other than /folder1 in your web root directory.

The [NE] flag is not needed, and you'll only need [P] if you plan to alias or re-rewrite the output of this rule. [L] is recommended for efficiency, again unless you plan to re-rewrite the output of this rule.

Jim

chy123

9:10 pm on Jul 2, 2009 (gmt 0)

10+ Year Member



Not sure what I've done wrong but I've tried both rules in both httpd.conf and .htaccess. The homepage one doesn't work for some unknown reason.

But I've got around it by adding a Rewriterule at the top:
RewriteRule ^/$ /new/home [P]

yes, I need the [P] because /new/home needs to be rewritten again a bit later on to send the url to another server.

At least it does work.

jdMorgan

3:14 am on Jul 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> The homepage one doesn't work for some unknown reason.

You've lost me there with loose language, I'm afraid... Please define "the hopepage one" and "doesn't work."

... could be as simple as the fact that ".+" means "match one or more characters, and a URL-path of "/" doesn't contain one or more characters past the "/".

Jim

chy123

9:58 pm on Jul 3, 2009 (gmt 0)

10+ Year Member



with homepage I meant the page linked to [domain.com...]

I have set up a DirectoryIndex and also DocumentRoot, but like I said before the code you gave me works good, just not for when I access [domain.com...]

But I have written a RewriteRule to catch / and put it at the top

jdMorgan

10:39 pm on Jul 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... could be as simple as the fact that ".+" means "match one or more characters, and a URL-path of "/" doesn't contain one or more characters past the "/".

Instead of adding "band-aid" rules, try using

 RewriteRule ^/(.*)$ /folder1/folder2/$1 [L] 

instead of
 RewriteRule ^/(.+)$ /folder1/folder2/$1 [L] 

for the /folder1/folder2/ rewrite.

You will also have to add another RewriteCond:

 RewriteCond $1 !^folder1/folder2/ 

to explicitly prevent looping with that new pattern.

Jim