Issues with: supress www. and add trailing slash in .htaccess #515

Closed
OllieW opened this Issue May 19, 2011 · 11 comments

6 participants

@OllieW

When using

Suppress "www." at the beginning of URLs

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>

Add trailing slash to (non-file) URLs

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]
</IfModule>

I type "www.example.com/test"
I am expecting "example.com/test/"
I am taken to "example.com/home/user/public_html/test/"

I type "www.subdomain.example.com"
I am expecting "subdomain.example.com/"
I am taken to "subdomain.example.com/subdomain/"

I type "www.subdomain.example.com/test"
I am expecting "subdomain.example.com/test/"
I am taken to "subdomain.example.com/home/user/public_html/subdomain/subdomain/test/"

NOTE the double /subdomain/subdomain/ preceeding the requested directory


I then removed

Suppress "www." at the beginning of URLs

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>

and left

Add trailing slash to (non-file) URLs

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]
</IfModule>

I type "www.example.com/test"
I am expecting "www.example.com/test/"
I am taken to "www.example.com/home/user/public_html/test/"

I type "www.subdomain.example.com/test"
I am expecting "www.subdomain.example.com/test/"
I am taken to "subdomain.example.com/home/user/public_html/subdomain/test/"

NOTE the lack of www. even though this is no longer specified in .htaccess and only one /subdomain/ preceeding the requested directory


Please note:

  • all the directories used had valid index.html files contained within.
  • I am on a shared host and do not have access to the Apache config files.

Am I doing something wrong or is it down to my hosting company?


On an aside to these issues...

Currently the prevention of directory listings is achieved through:

Options -Indexes

Would it not be more graceful to use:

IndexIgnore *

As seen in this tutorial, preventing a 404 from being thrown?


Thank you for your help.

@bicho44

I have the same issue, and only when the boilerplate are in a directory.
If i dont use the .htaccess, the problem its solve.
That's not the point, but...
I hope someone who master the .htaccess have a solution

@OllieW

Is this a common problem then or are we two isolated cases?
Has anyone else experienced this effect and if so are you on a local or a hosted server?

I believe the fault to be caused by the wildcard characters, i.e.

%1 and (.*)

I have tested a rewrite of these two code snippets on my remote shared host:

Suppress "www." at the beginning of URLs

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
</IfModule>

Add trailing slash to (non-file) URLs

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^example.com$ $1/ [R=301,L]
</IfModule>

Interestingly I obtain the following results...

I type "www.example.com/test"
I am expecting "example.com/test/"
I am taken to "example.com/test/"

SUCCESS

I type "www.subdomain.example.com"
I am expecting "subdomain.example.com/"
I am taken to "example.com/subdomain/"

FAIL subdomain prefix disappears and is instead appended to the end of the domain as if it were a directory
NOTE the index file to subdomain.example.com would actually display as when a subdomain is created a directory (with the same name as the subdomain) is created within the root, though I am not sure if this is specific to my server config?

I type "www.subdomain.example.com/test"
I am expecting "subdomain.example.com/test/"
I am taken to "example.com/subdomain/test/"

FAIL the same problem occurs as previously mentioned in that the subdomain pops up in the wrong part of the URL
NOTE as with the previous example an index page would show here for my server

Hopefully this can prove useful to someone in the know because I'm completely lost with this one

Thanks to anyone that can help find a solution and explain if these are isolated cases or part of a larger problem

@ghost

This is peculiar. Can you guys let everyone know what versions of Apache you're running?

@OllieW

I had a feeling it didn't make any sense...

I am running Apache Version 2.2.17 on a shared remote host so I have no control over config files other than my .htaccess file

It may well be that there are conflicts with settings higher in the hierarchy because I was experimenting with trying to gzip my content before I discovered HTML5 boilerplate and all the .htaccess mods I tried didn't work so I contacted my hosting company and they set it for me and said not to put anything about it in the .htaccess files because it would cause conflicts. Do you think this issue may be due to conflicts with the settings of my hosting company?

On an aside... none of the extra security stuff that is for php works in my .htaccess file, it throws a 500 server error. Does that require contacting my hosting comapny too?

@OllieW OllieW closed this May 22, 2011
@OllieW OllieW reopened this May 22, 2011
@OllieW

My apologies for closing the issue I clicked the wrong comment button... :$

@johandouma

Same issue here. Everything works again as expected when I remove
Options +FollowSymlinks

Running Apache/2.2.3 (CentOS)

@OllieW

Unfortunately when I removed

Options +FollowSymlinks

my issues were not resolved.

Are these errors due to the contents of the .htaccess file or are they due to other server config conflictions?

@alanhogan

Update: Not sure I understood the issue correctly. The title really ought to be reworded, as this issue isn’t a feature suggestion but rather an honest issue (problem), right?

@OllieW

Thanks for the update, you have understood correctly that this is an issue with the current .htaccess file and not a features suggestion. I have updated the title to be a little more clear.

@nedludd

This may belong as a separate issue, but it seems that this rule breaks UTF-8 encoded URIs.

Edit: I mean this rule:

# Option 1:
# Rewrite "domain.com/foo -> domain.com/foo/"

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ /$1/ [R=301,L]
</IfModule>

A URI like this:

"/households?utf8=%E2%9C%93&commit=member%27s+last+name%3F&search%5Blastname_like%5D=smith"

Get's converted to this via this rule:

"/households/?utf8=%25E2%259C%2593&commit=member%2527s+last+name%253F&search%255Blastname_like%255D=smith"

Note the extra percent sign (%25) in there.

Haven't figured out how it's getting in there yet, but it's definitely this rule.

@shichuan shichuan added a commit that referenced this issue Jun 23, 2011
@shichuan shichuan removed trailing slash redirects in htacess
a wiki page is created for people who want to use it: https://github.com/paulirish/html5-boilerplate/wiki/Proper-usage-of-trailing-slash-redirects
it is also under the Make it better section of the wiki https://github.com/paulirish/html5-boilerplate/wiki/Make-it-better
this has been discussed at many issues/tickets/mailinglist posts #493 #515
b37d518
@shichuan
H5BP member

guys, we have voted to remove trailing slash redirects in htacess by default, it's moved to wiki under 'make it better' section. for people who want to use it, please read: https://github.com/paulirish/html5-boilerplate/wiki/Proper-usage-of-trailing-slash-redirects

@shichuan shichuan closed this Jun 23, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment