Welcome to WebmasterWorld Guest from 54.196.233.239

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect to Add Slash

I need to add slash in large number of links

     

deezin

6:37 pm on Apr 16, 2013 (gmt 0)

10+ Year Member



Hello. I learned from Webmaster Tools that I have a lot of links going to the wrong place because there is a slash missing. How do I put a redirect in my .htaccess file to make something like this happen

change this: domain.com/directoryfilename.html

to this: domain.com/directory/filename.html

If you could help me, I'd be very grateful.

Thanks.
Rachel

lucy24

8:05 pm on Apr 16, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



We need way more information. Write the wrong rule and you'll be flooded with requests for
/director/yfilename.html
/direc/toryfilename.html
/directoryfil/ename.html
You can't ask Apache to stop, look at every page request letter by letter, and see whether it can be broken into multiple elements at any point. You need to know ahead of time where to look.

So the first question is: Does the problem apply to one specific directory? Or to a limited number of directories that htaccess can search for by name?

And the second question is: Where are these links coming from?
--If they are your own internal links, fix them right now. In fact, do this before you set up the redirect. Redirects are for other people's mistakes, not your own.
--If they are from people you're in contact with, similarly ask them to fix it. Search engines may or may not care-- but you can be pretty sure that a link leading to a redirect will not help you.

Third question: What's in your htaccess already? If you have anything at all involving mod_rewrite, your new redirect has to be configured as a RewriteRule. If you don't, it can be done with mod_alias (Redirect by that name).

deezin

2:08 am on Apr 17, 2013 (gmt 0)

10+ Year Member



Dear Lucy.

Thank you very much for your help.

This problem is happening to 2 directories that are being combined into one directory url (like seen below)

domain.com/directory1directory2/filename.html

when it should look like this

domain.com/directory1/directory2/filename.html

...somehow this happened when I was linking to these files in wordpress posts and the wordpress software was taking out the / in between the 2 directories. However, other sites have put these links on their sites - maybe the links used to work (these used to be functional because of a plugin that used to work but is no longer supported). It is a bit confusing to explain.

So Yes - the links are on other sites - too many to ask to make corrections. Other sites are linking to the correct versions of the url as well so we can't just change the directory name.

We have a LOT in our htaccess ... and yes, we already use mod_rewrite so it would have to be done with RewriteRule.

Thank you for your help...I know this is confusing to read. :)

Thanks.
Rachel

lucy24

4:00 am on Apr 17, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Just one directory pair? This is easy. As far as mod_rewrite is concerned, a directory slash is just another character like "a" or "b". Except the one after your domain name, duh. So in a top-level directory it's simply

RewriteRule ^directory1directory2/(.*) http://www.example.com/directory1/directory2/$1 [R=301,L]


or, if you prefer,

RewriteRule ^directory1(directory2/.*) http://www.example.com/directory1/$1 [R=301,L]


If there is stuff before the first directory, just include it in both sides of the rule:

RewriteRule ^starthere/directory1directory2/(.*) http://www.example.com/starthere/directory1/directory2/$1 [R=301,L]


Sure, you could capture and reuse the "starthere/directory1" part too-- but the server would expend more effort keeping the text in memory than it takes to read and write it over again. No point in capturing text if it can only say one thing.

I should have made you do that yourself, but everyone's been asking complicated questions and the people who speak Apache are nowhere to be seen and I'm annoyed with FutureBasic and, and, and...

g1smd

10:42 am on Apr 17, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Are all the valid directory1 names known?

If so then...
RewriteRule ^(dir1|dir2|dir3|dir4|dir5)(.*) http://www.example.com/$1/$2 [R=301,L]

should fix it.

Add another rule for
^(dir6|dir7|dir8|dir9|dir10)
and so on.

The above assumes that only one slash is missing per URL.

deezin

6:06 pm on Apr 17, 2013 (gmt 0)

10+ Year Member



Thank you so much to both lucy24 and g1smd. I will try one of these out and let you know if it works. THANK YOU!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month