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
-
Redirect 301 issue. I changed my domain name and Google is killing me.
Hello SEO community: I have this problem, and I don't know exactly what to do. I recently changed my domain from uclasificados.net to uclasificados.com uclasificados.net was a free classified ads for USA in spanish, and was my most affortable site, so I wanted to convert it to .com because I thought it could get more popular with the .com domain. uclasificados.com was before a free classified ads website for Colombia, but was not very popular and had poor traffic so I moved the Colombian content to uclasificados.co. Since I changed my domain from uclasificados.net to uclasificados.com I have lost a lot of ranking, and l my traffic every day is getting lower. I have already checked the 301 redirections and they are working correctly, but even thought I keep getting less traffic and less money. I have also checked with moz tools both sites link juice, and it says that uclasificados.net have better reputation. So I was wondering if I change it back and redirect uclasificados.com to uclasificados.net but I worrie that if I do that, maybe I can make things worse. What do you recommend me?
Technical SEO | | capmartin850 -
Does image domain name matter when using a CDN?
Has anyone does studies on using a different CDN domain name for images on a site? Here is an example: or ![](<a)http://cdn.mydomain.com/image.jpg> mydomain.com ranks highly and many images show up in Google/Bing image searches. Is there any actual data that says that using your real domain name for the CDN has benefits versus the default domain name provided by the CDN provider? On the surface, it feels like it would, but I haven't experimented with it.
Technical SEO | | findwell0 -
Domain Forwarding / Multiple Domain Names / or Rebuild Blogs on them
I am considering forwarding 3 very aged and valuable domain names to my main site. There were once over 100 blog posts on each blog and each one has a page authority of 45 and domain authority of 37. My question is should i put up three blogs on the domains and link them to my site or should i just forward the domains to my main site? Which will provide me with more value. I have the capability to have some one blog on them every day. However, i do not have access to any of the old blog posts. I guess i could scrape it of archive.org. Any advice would be appreciated. Scott
Technical SEO | | WindshieldGuy-2762210 -
Should this site start again on a new domain
Hi We have not done SEO on this site they have used another company who looks like they outsourced and the links have been built by a third party all blog networks and this company have said they cannot get the links removed. Google flagged artificial links on this web site in February and in April it lost over 10000 visitors in a month and its just free falled ever since. The categories have been recreated and no redirects created due to the amount of backlinks from the blog sites to the original category pages but the site is not recovering its down to 1500 visitors a month and used to get 14000 a month. So should my customer ditch the domain and move this site to fresh domain? http://www.kids-beds-online.com Any answers would really be appreciated. thanks Tracy
Technical SEO | | dashesndots0 -
Are sub domain names a good idea
Hi i have read some info that says that sub domain names help with increase traffic but i am not sure if this is true. I would like to know if i should use a subdomain name to increase the size of my website and increase traffic. I have also read that it is a good idea to put a blog on a subdomain name. Can anyone share their experience and let me know if they have found that sub domains are a good idea
Technical SEO | | ClaireH-1848860 -
Multi-Word-Keyphrase in domain name wo/ or with dashes?
SEO Gurus, There seems to be a tendency that whenever you need to optimize a project for a multi-word key phrase, lets say for example "hostels in boston" SEOs see it as a best practice to refelct the key phrase in the domain without dashes yet when being used in a directory/page name context dashers are being used? Does anyone have any experience to share on this topic what works better? From my experience using dashes has been quite successful in the past but I am questioning this approach for a new project I am about to start. To clarify the question, in your eyes what would work better for the keyphrase "hotels in boston" www.hotelsinboston.com www.hotels-in-boston.com Thanks /Thomas
Technical SEO | | tomypro0 -
Subfolder to Root Domain, Should i or not?
Dear All, I am kinda of stuck and need your helps, I have major client's site which is located in subdirectory. Instance: http://www.majorclient.com.cn/cn/ and as you can see domain name is already localized and they are not planning to make any english version of the site, So there will be no /eng/ folder but clients whole website is /cn/ subdirectory which is totally unnecessary and i would like to 301 whole site to **http://www.majorclient.com.cn ** But the problem is that this site has been /cn/ folder forever since the beginning and lot of trusted links are pointing to www.majorclient.com.cn/cn/ So should i move it or just configure 301 www.majorclient.com.cn to www.majorclient.com.cn/cn/ leave it there and don't bother. Help?
Technical SEO | | DigitalJungle0 -
Duplicate content across multiple domains
I have come across a situation where we have discovered duplicate content between multiple domains. We have access to each domain and have recently within the past 2 weeks added a 301 redirect to redirect each page dynamically to the proper page on the desired domain. My question relates to the removal of these pages. There are thousands of these duplicate pages. I have gone back and looked at a number of these cached pages in google and have found that the cached pages that are roughly 30 days old or older. Will these pages ever get removed from google's index? Will the 301 redirect even be read by google to be redirected to the proper domain and page? If so when will that happen? Are we better off submitting a full site removal request of the sites that carries the duplicate content at this point? These smaller sites do bring traffic on their own but I'd rather not wait 3 months for the content to be removed since my assumption is that this content is competing with the main site. I suppose another option would be to include no cache meta tag for these pages. Any thoughts or comments would be appreciated.
Technical SEO | | jmsobe0