Forum Moderators: phranque

Message Too Old, No Replies

Trailing slash issue on dir rewrite

And wired www vs non-www issue associated with it

         

Tastatura

9:05 am on Apr 4, 2006 (gmt 0)

10+ Year Member



sub title should state : And weird www vs non-www issue associated with it
Hi all,
I an newb when comes to mod_rewrite; I spent quite few hours reading documentation and going through forum, as well trying different solutions, but can’t get it to work (either condition is not recognised or I end up getting 403, 404 or even 500 errors …)
What I want to do is not show index.html after sub-dir is called, as well as not to show file extensions on any file in the sub dir.

So a call to page
[mydomain.com...]
Should display
[mydomain.com...]

and
[mydomain.com...]
should display
[mydomain.com...]

as you might noticed I am going without www prefix(or sub-domain), and I (think I) addressed that in site’s root .htaccess file

In browser’s bar, if I type ‘http://mydomain.com/subdir/index.html’ it gets redirected and displayed as expected (same for other pages). Also If I type in ‘http://mydomain.com/subdir/ ‘ all is good. However if I type [mydomain.com...] (note no trailing slash) correct (index) page gets displayed, however URL shows as [mydomain.com...] - note WWW. I don’t want www due to consistency and dup content issue. Any suggestions how to address this are greatly appreciated.

This is how my sub dir .htaccess looks like

addhandler application/x-httpd-php .htm .html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^/]+/)*([^./]+)/?$ $2.html [L]

And this is how my site’s root .htaccess looks like


Options All -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteRule ^(.*)$ [mydomain.com...] [R=301]
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^/]+/)*([^./]+)/?$ $2.html [L]

P.S. My site does NOT have a file named subdir in the main root directory

Tastatura

3:14 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



bumpy. anyone?

extras

3:28 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



With vanila installation of Apache, it shouldn't happen, IMO.
So, I guess your host (or the server) is configured it to add www,
when there is no slash at the end.

Putting this in the .htaccess may solve it.
(This code preserves the domain name and add trailing slash.)

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

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




You may have to use -d check version, which is a bit heavier,
if it doesn't work well with your site.

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule [^/]$ [%{HTTP_HOST}%{REQUEST_URI}...] [L,R=301]

If you want to cover both http and https:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteCond s%{HTTPS} ^((s)on¦s.*)$ [NC]
RewriteRule [^/]$ http%2://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]

jdMorgan

7:12 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I thought I posted to this thread earlier -- I guess not.

The basic problem is that the [L] flag is missing in the redirect in the originally-posted code. Therefore, mod_rewrite defers the external redirect, falls into the next rewrite directive, changes the local URL-path to the internal script path, and then does the redirect, thus exposing the internal rewrite.

Again, always use [L] unless you are absolutely sure you don't need it.

Jim

Tastatura

10:14 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



Extras and Jim,
Thanks a lot for the help and info.
Here is what I got (and I made sure to empty browser cache on each try)
Jim,
I added L flag to the root’s .htaccess file (located in the /public_html/ folder on my shared Apache 1.3 server), however it did not do the trick. When I typed in [mydomain...] I still got redirected to [mydomain...] . Subdir’s .htaccess was as originally shown in the first post. For completeness, this is how modified root .htaccess ‘ looks like after edit

Options All -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
#added L flag to the following rule
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+/)*([^./]+)/?$ $2.html [L]

Extra,
It worked after I added your code , with the –d check version, to the subdir’s .htacess file. Thanks!

(I think) I understand what Jim is saying about redirects in msg #4 – it makes sense, but it did not work for me; I would like to learn, so I am curious to know why it didn’t

[edited by: jdMorgan at 3:02 am (utc) on April 15, 2006]
[edit reason] obscured specifics. [/edit]

extras

1:35 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



It's caused by mod_dir, IMO.

According to someone who had similar issue,
mod_dir uses the domain name defined by ServerName (in virtualhost).

So, if it's www.example.com, (with ServerAlias set to example.com?)
mod_dir would redirect to www.example.com when the URL is for a directory and there is no trailing slash.

I thinl it's more common to set ServeName to example.com and set ServerAlias to *.example,com.
So, mod_dir would remove www with the redirect, and causes double login problem and other unexpected results.

In his casem he solved the problem by changing the ServerName.

However, on shared hosting and some other situation,
it's not possible or practical to change ServerName
and I think we have to use RewriteRule for removing or adding www.

On Apache2.0.51 and later, there is a DirectorySlash directive to stop mod_dir from redirecting the URL without enfing slash.
[httpd.apache.org...]

But it's not the same thing as you can do with RewriteRule.