Forum Moderators: phranque

Message Too Old, No Replies

Tweaking a simple subdomain rewrite

         

astgtciv

6:51 am on Jan 21, 2006 (gmt 0)

10+ Year Member



Hi, guys! This looks like a very nice forum!

I am just starting out with mod_rewrite, overwhelmed by the voodoo docs :) Found a lot of useful stuff here.

What I am doing is a simple subdomain->subdirectory rewrite. The rewrite works fine, the only thing that I would like to tweak is the following: when a request comes for a directory (and the corresponding index.html is served), the address in the browser address bar changes to include the subdirectory.

Example: I am rewriting [new.com...] to the folder "new" on [old.com....] If a request is made for [new.com...] it's served correctly from [old.com...] but the address in the browser still says [new.com...] (perfect!). However, if a request is made for [new.com...] , then the address in the browser changes to [new.com...] , and then the corresponding index.html is served. Is this somehow due to an internal request apache makes? I tried adding on an "index.html" myself for directory requests, but it didn't work...

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ $1/index.html

My main rewrite is the usual (.htaccess):

RewriteCond %{HTTP_HOST} new\.com$ [NC]
RewriteCond %{REQUEST_URI}!^/new/.*
RewriteRule ^(.*)$ /new/$1 [L]

Any help greatly appreciated! Thank you!

extras

7:38 pm on Jan 21, 2006 (gmt 0)

10+ Year Member



This is one of the missing trailing slash problems.

By adding this before other rules, it should go away.


Options +FollowSymlinks
RewriteEngine On
RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

If you want to cover both http and https:
Options +FollowSymlinks
RewriteEngine On
RewriteCond s%{HTTPS} ^((s)on¦s.*)$ [NC]
RewriteRule ^/*(.+/)?([^.]*[^/])$ http%2://%{HTTP_HOST}/$1$2/ [L,R=301]

Note: These codes are very efficient compared to the code with '-d' check,
but they won't work with directories that have a dot (period) in it.
(ex. /a_directory.name/)

astgtciv

1:27 am on Jan 22, 2006 (gmt 0)

10+ Year Member



Awesome! Thank you, extras! I got greedy and tried the HTTP/HTTPS code (even though I only have HTTP ;)) - which didn't work... But the HTTP code alone worked perfectly! I have no directories with dots in them, so I am all set. Thanks so much again!