Forum Moderators: phranque
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
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]
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
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]
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.