Forum Moderators: phranque

Message Too Old, No Replies

Setting up a no-www dir structured URL without a trailing slash

         

Cor van Noorloos

6:46 pm on Jul 24, 2010 (gmt 0)

10+ Year Member



Hello,

Some time ago I've been trying to set up a vBulletin + vBSEO forum using dir structured URLs but without letting these end with a trailing slash.
Trying to use this setup for a new forum I now realize manually adding a trailing slash to an URL leads to a 404 error.

http://example.com/foo OK
http://example.com/foo/ 404


After lots of digging I noticed below solved this issue

# Remove and 301 redirect trailing slashes
RewriteEngine On
RewriteCond %{HTTP_HOST} !^\.example\.com$ [NC]
RewriteRule ^(.+)/$ http://example.com/$1 [R=301,L]


However, it now somehow interferes with my current setup below which makes my site class B (using a no-www setup)

# Make your site Class B. http://no-www.org/
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]


Would it be possible to somehow combine these perhaps?

g1smd

8:14 pm on Jul 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} !^\.example\.com$

This condition will never match. The requested host name will never BEGIN with "dot example dot com"

Cor van Noorloos

8:54 pm on Jul 24, 2010 (gmt 0)

10+ Year Member



Hi g1smd,

Thank you for your quick reply.

I have to admit though I'm not quite sure on what you're trying to explain.

Trying to explain my situation better a live example. Note that I have commented out the no-www bit from my .htaccess file.

http://vbdock.com/user-titles-update-3/
currently redirects to
http://vbdock.com/user-titles-update-3
, now avoiding any 404 errors

# Remove and 301 redirect trailing slashes
RewriteEngine On
RewriteCond %{HTTP_HOST} !^\.vbdock\.com$ [NC]
RewriteRule ^(.+)/$ http://vbdock.com/$1 [R=301,L]


Both
http://www.vbdock.com/user-titles-update-3/
and
http://www.vbdock.com/user-titles-update-3
however aren't redirecting to
http://vbdock.com/user-titles-update-3
, which is the way I would like to see it (avoiding any duplicate content)

g1smd

10:59 pm on Jul 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The
^
operator means "
begins with
" so that pattern is trying to match a request for
http://
.vbdock.com
and the ! operator means "not".

The redirect will run for every request, because the pattern will never match any request.

Cor van Noorloos

7:59 pm on Jul 25, 2010 (gmt 0)

10+ Year Member



Again, much appreciated.

RewriteEngine On
# Make your site Class B. http://no-www.org/
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
# Remove and 301 redirect trailing slashes
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.+)/$ http://example.com/$1 [R=301,L]


seems to do all that is wished for. Could you perhaps tell if it would be required to optimize this rule any further?

Ie. would it be possible to tell this rule to do this with directories right away, instead of guessing it's a file first?

jdMorgan

8:22 pm on Jul 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



These two rules are not in the correct order.

If ordered correctly, the RewriteCond on your currently-second rule is not needed.

The hostname canonicalization rule only works for the www subdomain. Do any others resolve to your server?

It also cannot handle FQDN formatted hostnames, or appended port numbers, and so is not 'fully effective.'

With your 'remove slash' rule in place, it will be impossible for you to add HTTP-accessible subdirectories to this site. Are you sure that won't ever be a problem?

Jim

Cor van Noorloos

8:47 pm on Jul 25, 2010 (gmt 0)

10+ Year Member



Hi Jim,

Thank you for your reply. I'm perfectly happy with no subdomains.

The URLs are already available without a trailing slash added.

Just in case someone manually adds a slash they should be redirected to one without. Which also -if I understand correctly- avoids any duplicate content.
As this is all new to me, I hope you understand.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
# Make your site Class B. http://no-www.org/
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
# Remove and 301 redirect trailing slashes
RewriteRule ^(.+)/$ http://example.com/$1 [R=301,L]


I've left the order intact, and removed the first condition instead, which all seems to work :)

g1smd

11:20 pm on Jul 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Reverse the order. A www request with a slash will otherwise pass through a double redirect. Any incorrect URL should pass through just a single redirect to correct it. Reversing the order fixes that.

jdMorgan

11:44 pm on Jul 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There were two questions above. OK, no subdomains. But what about subdirectories?

It is common to have the "/w3c" subdirectory for compact privacy policies, and subdirectories for stats, images, scripts, etc... The privacy policy may be of particular concern if you market within the EU.

So again, are you quite sure you will never need any subdirectories?

... just trying to get the requirements nailed down before getting into specifics, here.

Jim

Cor van Noorloos

7:58 am on Jul 26, 2010 (gmt 0)

10+ Year Member



Hi Jim,

Thanks again.

I am fine with no subdomains. My site however (vbdock.com) does contain subdirectories.

If I understand correct
http://vbdock.com/uploads/2010/05/
won't be acciseble anymore.
However, I can still reach
http://vbdock.com/uploads/2010/05/vbulletin-performance-before.png

jdMorgan

3:03 pm on Jul 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes. correct.

You have three choices: Either create a 'list' of subdirectories that you need to access, or check each request for "directory exists" before removing the trailing slash. The first approach requires more research and maintenance, while the second approach requires much more CPU time and wear-and-tear on your hard drive.

You can also combine the two approaches, checking the disk only if the trailing slash is present and the directory is not on the exclusion list.

In general if the site is 'mature and stable,' the first option is most efficient. Otherwise, this last option is the best trade-off between server performance and low maintenance. Example:

RewriteEngine on
#
# Externally redirect to remove trailing slashes if not a known
# subdirectory index and requested subdirectory does not exist
RewriteCond $1 !^(images|cssfiles|awstats|uploads)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://example.com/$1 [R=301,L]
#
# *** NOTE*** Use this rule if possible, otherwise delete it, then un-comment and use the next one...
# Externally redirect all non-blank non-canonical hostname requests to the canonical hostname
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#
# Externally redirect requests for most non-canonical "example.com"
# hostname variants to the canonical hostname
# RewriteCond %{HTTP_HOST} ^www\.example\.com [NC,OR]
# RewriteCond %{HTTP_HOST} ^example\.com(\.|\.?(:[0-9]+))$ [NC]
# RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Jim

Cor van Noorloos

4:38 pm on Jul 26, 2010 (gmt 0)

10+ Year Member



Thanks Jim, this seems to do the trick and more. Much appreciated both.