How to force a trailing slash after the domain name
-
My campaign analysis is predictably listing domain.com and domain.com/ as repeated content. I've searched and searched but cannot find a way to force a trailing slash on the end of the domain name unless there's a file or directory after it..
Is there a way to accomplish this using .htaccess
-
I've gone with this .htaccess from your soulgorithm.com:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.co.uk [NC]
RewriteRule (.*) http://domain.co.uk/$1 [L,R=301]RewriteCond %{REQUEST_URI} (.)/$
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.)/$ $1.php [L]RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]and I'm now getting the results I'm after. I'm getting similar behaviour to you in Firefox and IE, which explains a lot. I really appreciate the length you've gone to to help me here, so big thank you!
-
Test Site: soulgorithm.com
In the .htaccess file for this site:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.soulgorithm.com [NC]
RewriteRule (.*) http://soulgorithm.com/$1 [L,R=301]RewriteCond %{REQUEST_URI} (.)/$
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.)/$ $1.html [L]RewriteCond %{REQUEST_URI} (.)/$
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.)/$ $1.php [L]RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f [OR]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]Which has the following effect:
soulgorithm.com > soulgorithm.com/
(slash is added, but only shows in IE and looks
likes its being stripped by Firefox but page
still loads fine)
soulgorithm.com/ > soulgorithm.com/
(loads fine, but only shows in IE and lookslikes its being stripped by Firefox but page
still loads fine)
soulgorithm.com/test > soulgorithm.com/test/
(loads fine, slash even shows in FF)
soulgorithm.com/test/ > soulgorithm.com.com/test/
(loads fine)
soulgorithm.com/testdir > soulgorithm.com/testdir/
(loads fine, slash even shows in FF)
soulgorithm.com/testdir/ > soulgorithm.com.com/testdir/
(loads fine, slash even shows in FF)
Let me know if this is what you see. I feel likes its getting close to working.
-
Thanks for sticking with this. Rather than me share the domain, do you know of any example sites using your code (or similar) which add a trailing slash after the domain name? I'd like to rule out my browser stripping it out.
-
Man, my mind is blown right now. I'm not giving up and hopefully someone else can chime in on this discussion and shed some light on this issue.
The code provided should have worked. Let me look into it some more. Also, if you don't mind what is the actual domain name?
-
That's right - nothing in there but the code you supplied.
-
Is this the only thing you have in your htaccess file?
if not, I would remove everything in the file and only have what i posted above, and let me know if it works.
-
Nope. Still no trailing slashes being added.
-
Try just the following:
Let me know if this works for you.
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !index.php RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*)$ http://www.domain.com/$1/ [L,R=301]
-
Thanks for the reply, but this looks like all the other examples I've found. My .htaccess file looks like this :
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.)/$
RewriteRule ^(.)$ http://domain.co.uk/$1/ [L,R=301]But I get the following redirects going on:
domain.co.uk > domain.co.uk (ie nothing happens)
domain.co.uk/ > domain.co.uk (ie slash is removed)
domain.co.uk/page2 > domain.co.uk/page2 (ie nothing happens, but page loads)
domain.co.uk/page2/ > Internal server error
Any ideas?
-
Hi Clive.
Yes, you can easily do this with an .htaccess file, here is the code:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
Just replace "domain.com" with your proper url for your site. This should be all that is needed.
Hope this helps!
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
Hey all -- ever seen a client with URLs that keep repeating the domain? Something like: client.com/client.com/client.com/subfolder-name. Any idea what glitch could cause that?
Hey all -- ever seen a client with URLs that keep repeating the domain? Something like: client.com/client.com/client.com/subfolder-name. Any idea what glitch could cause that?
Technical SEO | | TDC_SEO0 -
How should I set up a domain redirect
A client has 2 domains that he wants to use for the same site. At the moment one domain is just an abbreviation of the main domain (not sure why) as follows: www.mygreatpropertycompany.com
Technical SEO | | davidmaxwell
www.mgpc.com (just redirects to the above) He is complaining that when he searches for 'mpc' there are no results (at all) so I'm wondering what the best approach is.There is currently nothing on the main domain that refers to 'mgpc' in it's abbreviated sense - the only place it's being used is the company's email addresses (info@mgpc.com). The redirect is simply a html file in the root of www.mgpc.com as follows: Is there anything I can do to help him out? (this is one of those 'doing a friend a favour' tasks!) Thanks!0 -
Best practice for multiple domain links
A site i'm working on has about 12 language domains - .es, it, .de etc. On each page of every domain the header has links to every homepage. At the moment these are all set to no-follow as an initial step to stop potential link profile issues spreading around. Moving forward i'm not totally sure how to handle these links. On one side I see and agree that no-follow is not necessary, but do-follow is just filtering out and weakening link juice. What is the best way to handle this scenario?
Technical SEO | | MickEdwards0 -
Redirect to new domain.... (which is penalised)
Hey - how's things? I have a client who wants to redirect his main domain to a new one.... there are a couple of problems I see and thought I'd ask on moz. 1 - The new domain has been incorrectly parked on the old domain with no redirection in place... when you do "site:domain.com" in Google, there are no serps for the new domain (the old domain still ranks well), it doesn't seem to rank anywhere and doesn't return any results in OSE. Is it wise to redirect to this domain or will rankings drop on both? 2 - The new domain uses .mobi as its suffix and will be replacing a .com - but is much more related to the business keyword wise. Is using mobi a problem. Overall the SEO on the site is abysmal and I will be reworking everything - so there will be lots of changes going on at the same time. I'm just wondering if it is worth redirecting the new domain at all, or trying to get brand new domain and use that.... or just to stick with the original aged domain... I think that is my only concerns at the moment
Technical SEO | | agua0 -
Rankings of Subdomains vs. Main Domain
Here's a puzzler... Our main domain (www.ides.com) doesn't appear in Google (but does on Bing and other engines). We think this is due to duplicate content which we're fixing. However our website's subdomains continue to appear prominently in SERPs, even on Google - here are some examples: IDES Prospector = prospector.ides.com IDES = support.ides.com Cycolac FR15 = catalog.ides.com Why would Google penalize a main domain and its subdomains?
Technical SEO | | Prospector-Plastics0 -
Blog.domain.co.uk or domain.co.uk/blog
Hi Guys, I'm just wondering which offers more SEO value and which is easier to set up out of: blog.domain.co.uk domain.co.uk/blog Thanks, Dan
Technical SEO | | Sparkstone0 -
Issues with trailing slash url
Recently, we have changed our website to www.example.com/super-rentals/ (example) and we have done a 301 redirection to the new urls from the old one. We have noticed in Google webmaster tool that urls without trailing slash as 404 error. www.example.com/super-rentals. Please let us know how to fix this issue as soon as possible. Note: Our previous urls are not the urls without trailing slash. It is a different url (www.example.com/super-rentals.htm) we have rewritten in to www.example.com/super-rentals/ only. I would like to know why GWT pulls out the urls without trailing slash and shows in 404 error. Thanks for your time
Technical SEO | | massimobrogi0 -
Web config redirects not working where a trailing slash is involved
I'm having real trouble with getting working redirects in place to use on a site we're re-launching with a modified url structure. Old URL: http://www.example.com/example_folder/ New URL: http://www.example.com/example-of-new-folder/ Now, where the old URL's have a trailing slash the web.config simply will not accept it. It says the URL can start with a slash, but not end with a slash. However, many of my URL's do end with a slash so I need a workaround. These are the rules I'm putting in place: <location path="example_folder/"></location> Thanks
Technical SEO | | AndrewAkesson0