Forum Moderators: phranque

Message Too Old, No Replies

Need help with mod_rewrite and redirecting URLs with no trailing slash

         

georgec

6:14 am on May 6, 2005 (gmt 0)

10+ Year Member



Hi:
I have a problem hopefully mod_rewrite can help solve. I have a subdomain called test.mysite.com that's hosted on a different server than mysite.com (using a A record). The subdomain works just fine, except for I noticed if I type:

[test.mysite.com...]

it DOESN'T work, while:

[test.mysite.com...] (with trailing slash)

does. This is true for any sub directory underneath the subdomain. I've asked both the hosts of the domain and subdomain, but they each just kick the problem back and forth.

Can I set up a .htaccess file on the subdomain so it detects and redirects any sub directories with no trailing slashes to the exact URL but with a slash? I figured that's easier than to figure out what the problem might be DNS wise.

Thanks!

George

jd01

7:11 am on May 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi George,

The simple answer to your question is... Yes.

The slightly longer answer (to the question I am sure you will have next: How?) depends on what your actual structure is now.

Is your site only 2 levels deep (/yoursite.com/dir/) or are there more levels?

Do you have requests that end in file extensions (/dir/file.html) or are they all simply /dir/file or /dir/file/. (The ruleset you will need to use will varry depending on this)

Are you using any rewrites now?

Hope this helps.

Justin

georgec

7:26 am on May 6, 2005 (gmt 0)

10+ Year Member



Hi jd01:
Thanks for the reply. Regarding the details:

1) It's possible some directories will be more than 2 levels deep, though I'd be happy right now with a solution that deals just with one level (ie: test.mysite.com/dir) if it's more difficult the other way around.

2) Yes, there will be actual file requests, whether it's pages (ie: test.mysite.com/file.html) or images etc.

The server is WHM/ cPanel based, and I've gotten mod_rewrite working as far as fighting hot linking.

Thanks!

George

jd01

10:17 am on May 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi George,

Since you are using only 1 level, this should be close to what you need:

RewriteEngine ON
RewriteRule ^([a-z]+)$ /$1/ [R=301,L]

As long as your files are all letters, this should catch the file name, but eliminate files, because the regular expression only matches letters, not any .(dot) characters. By putting the 'hard' ending prior to matching any / character, anything that contains a / should also fail to match.

I looked at another version with conditions, but it seemed like it would waste more processor overhead... It's a little late where I am, so if my logic is failing (IOW your rule doesn't work) please, let me know and I will have another look at it when I am a little more fresh.

Hope this helps.

Justin

jdMorgan

1:39 pm on May 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the general case, allowing multiple directory levels, this should work:

RewriteCond %{REQUEST_URI} !^[^.]*/$
RewriteRule (.+) /$1/ [R=301,L]

For any URL-path which is NOT(any number of any characters except for "." ending with a slash) append a slash to the URL-path, and redirect.

Test cases:

GET / -- no redirect because path ends with / (this also fails to match the RewriteRule pattern in .htaccess context)
GET /subdir/ -- no redirect, because path ends with /
GET /index.html -- no redirect, because path contains "."
GET /subdir/index.php -- no redirect, because path contains "."
GET /subdir/index.cgi/ -- no redirect, because path contains "." and ends with /

GET /subdir -- redirect, because path contains no "." and does not end with /
GET /subdir/subdir -- redirect, because path contains no "." and does not end with /

Jim

georgec

5:55 pm on May 8, 2005 (gmt 0)

10+ Year Member



Thanks guys. I've tried both suggestions above, though I still get a "page not found" for non slash URLs. I think perhaps the problem occurs on the DNS level, and .htaccess may be too late in intercepting the problem.

A new question- as mentioned, my subdomain test.mysite.com is created by adding a A record to the DNS Zone of Server A, so it looks something like:

test A Record 12.12.12.12

where "12.12.12.12" is the IP of server B actually hosting test.mysite.com Do I need to add another A record like so:

www.test A Record 12.12.12.12

So [test.mysite.com...] also works? I think non slash URLs are being redirected to this URL instead. Thanks,

jdMorgan

6:59 pm on May 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I think non slash URLs are being redirected to this URL instead.

Why do you think that is happening? -- Do you have code that does this? (It won't happen by magic)

If you want "www.example.com" to resolve, yes, you will need an A record.

All of the code above assumes that you already have other mod_rewrite code working in the same file. If not, it's possible you may need to add:


Options +FollowSymLinks
RewriteEngine on

ahead of the code. Otherwise, no rewriting will take place.

Jim