Welcome to WebmasterWorld Guest from 3.227.3.146

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

mod_rewrite - how to combine rules?

Adding a trailing slash, plus redirecting to a .php file

     
12:42 am on Jan 8, 2004 (gmt 0)

New User

10+ Year Member

joined:Jan 8, 2004
posts:24
votes: 0


Having read through many helpful posts in this forum, I have found out how to achieve two effects using a .htaccess file, but I would like to combine them, and am unsure if I need a RewriteCond, and a RewriteRule; two RewriteRules etc.

Essentially, the behaviour I am trying to get is:

1. Resubmit URL (after site rebranding), adding a trailing slash if there is none.
2. Have the URL request sent to a single .php file (technique outlined in http://www.alistapart.com/articles/succeed/)

RewriteEngine on
RewriteRule ^XYAZ/(.+[^/])$ /XYZ/$1/ [R]

This successfully maps http://www.myserver.com/XYAZ/events to http://www.myserver.com/XYZ/events/

RewriteEngine on
RewriteRule!\. (gif¦jpg¦css)$ xyz.php [L]

This successfully directs http://www.myserver.com/XYZ/events/ to xyz.php (which then dynamically serves the content based on the final element of the URL - hence the need for the trailing slash).

RewriteEngine on
RewriteRule ^XYAZ/(.+[^/])$ /XYZ/$1/ [R]
RewriteRule!\. (gif¦jpg¦css)$ xyz.php [L]

With this in .htaccess, http://www.myserver.com/XYAZ/events returns "400 Bad Request. Your browser sent a request that this server could not understand".

Any ideas how I can combine the two behaviours please?

Many thanks,

Nick.

[edited by: jdMorgan at 2:38 am (utc) on Jan. 8, 2004]
[edit reason] de-linked [/edit]

1:55 am on Jan 8, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


nickknowledge,

The following rewrites are done in stages so that they do not depend on each other - that is, missing slashes are added for directory requests in all domains, XYAZ.com is then redirected to XYZ.com, and finally the appropriate page requests are delivered to your xyz.php script - again, for all domains.

If you try to combine all these, then requests which *do* have a trailing slash, or those made directly to XYZ.com would not be delivered to your script. Image and CSS file requests made to XYAZ would not be redirected to XYZ.com... These are just examples of why the rewrites must be independent of each other.


RewriteEngine on
#
# IF URI does not end with "/"
RewriteCond %{REQUEST_URI} !/$
# and IF URI does not end with <anything> <dot> <something>
RewriteCond %{REQUEST_URI} ![^.]*\.[^.]+$
# THEN append trailing slash and redirect the client
RewriteCond %{HTTP_HOST} (.+)
RewriteRule (.*) http://%1/$1/ [R=301,L]
#
# Externally redirect XYAZ.com to XYZ.com
RewriteCond %{HTTP_HOST} ^(www\.)?XYAZ\.com
RewriteRule .* http://www.XYZ.com [R=301,L]
#
# Internally redirect all except image, CSS, and script requests to xyz.php
RewriteRule !(\.gif¦\.jpg¦\.css¦xyz\.php)$ /xyz.php [L]

A simpler solution to the trailing slash problem may be to ask your host to set UseCanonicalName off.

If you have files whose names do not include a file type (extension), then you will have to use the -d and/or -f flags of RewriteCond to tell files and directories apart. Otherwise, the simple test for a dot in the filename shown above should work fine.

Change all the broken pipe characters back to solid pipes before use - posting here changes them.

Jim

3:57 am on Jan 8, 2004 (gmt 0)

New User

10+ Year Member

joined:Jan 8, 2004
posts:24
votes: 0


Jim,

Thank you so much for that. Worked like a charm first time.

Pages like [xyz.com...] and [xym.com...] work.

[xyz.com...] doesn't work (but [xyz.com...] does)

Do I need to somehow test that %{REQUEST_URI} is not the root? If so, how would I do that please?

Many thanks,

Nick

4:14 am on Jan 8, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


I'm sorry, I can't follow you...

What does not work? Which function is failing and how?
(Basically, what do you type, and then what do you see as a result?

(I should add that there may not be a solution, since you normally should not have to do anything about trailing slashes - that should be taken care of automatically, but isn't. This indicates a possible server set-up problem.)

Jim

4:17 am on Jan 8, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Also, I goofed this part above - see corrections below.
[code]
# Externally redirect XYAZ.com to XYZ.com
RewriteCond %{HTTP_HOST} ^(www\.)?XYAZ\.com
RewriteRule (.*) [XYZ.co[b]m...] [R=301,L]
[/code
4:23 am on Jan 8, 2004 (gmt 0)

New User

10+ Year Member

joined:Jan 8, 2004
posts:24
votes: 0


[myserver.com...] - serves the "Test Page for Apache Installation".

[myserver.com...] - serves home page

[myserver.com...] - serves home page.

It seems that it's only happy serving root when a second trailing slash is appended. Any ideas?

Thanks for clearing up the XYAZ to XYZ translation.

Regards,

Nick

3:19 am on Jan 9, 2004 (gmt 0)

New User

10+ Year Member

joined:Jan 8, 2004
posts:24
votes: 0


Seems that it wasn't an apache issue after all.

Serving local content, IE6 renders [localhost...] correctly as the home page, but Mozilla Firebird 0.7 doesn't (displays Test Page for Apache Installation). When the content is uploaded to the web, Firebird serves the root content fine.

Thanks for all your help.

Nick.

3:39 am on Jan 9, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Hmmm...

You might want to try playing with Tools->Options->General->Connection Settings->No proxy for: settings, and either add or remove the Localhost entry.

Jim