Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect problem. script adding trailing slash

Problems redirecting webpages using .htaccess

         

Muji_san

9:55 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



I've spent so many hours trying to fix this problem and finally decided to try posting it on this board.

I'm relatively new to webmastering so please forgive any stupid questions.

What I want to do:
I would like to redirect a webpage from (an example)
www.example.com/thispage

to

www.example.com/otherpage


What I am using now:

I have made a .htaccess file for my root folder
with this script.
(.htaccess being the most search engine friendly way of doing this, or at leas I read.)

Redirect 301 /thispage http://www.example.com/otherpage/otherpage.htm

The problem:
Seems to work ok except for the fact that the script ADDS A TRAILING SLASH to the redirected address which leads to 404 missing page.

Redirects me to
http://www.example.com/otherpage/otherpage.htm/

instead of

http://www.example.com/otherpage/otherpage.htm
(without the trailing slash)


Is this a common problem? Any help would be greatly appreciated.

Thanks. Muji

wildbest

10:05 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



Redirect 301 /thispage http://www.example.com/otherpage/otherpage.htm

You want to redirect directory to file or do you mean:
Redirect 301 /thispage.htm http://www.example.com/otherpage/otherpage.htm

Muji_san

10:18 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



I have links on several websites that go to this page

www.example.com/thispage.htm

I've changed the name of the page (not foreseeing this problem) and would like the above link to go to the new page

www.example.com/otherpage.htm

The problem is that the script redirects me to the new page AND ADDS a trailing slash

www.example.com/otherpage.htm/

which leads me to a missing page error.

wildbest

10:28 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



So your target is:
http://www.example.com/otherpage.htm
not:
http://www.example.com/otherpage/otherpage.htm

Try this:
Redirect 301 /thispage.htm http://www.example.com/otherpage.htm

Muji_san

10:40 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



oops. sorry. my mistake on the example.
The correct links...

I have links on several websites that go to this page
www.example.com/thispage.htm

I've changed the name of the page (not foreseeing this problem) and would like the above link to go to the new page

www.example.com/otherpage/otherpage.htm

The problem is that the script redirects me to the new page AND ADDS a trailing slash

www.example.com/otherpage/otherpage.htm/

which leads me to a missing page error.

Wildbest- Thanks for your suggestion. That's the structure of the code I am presently using. but the script leads to an address and ADDS a TRAILING SLASH.

wildbest

10:47 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



Muji_san, in your original post you say you use this code:
Redirect 301 /thispage http://www.example.com/otherpage/otherpage.htm

I've suggested you use this code:
Redirect 301 /thispage.htm http://www.example.com/otherpage/otherpage.htm

Did you try what I've suggested or not?

Muji_san

10:54 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



Sorry Widbest.

Missed that little addition. (.htm) in your post.
(learning that precision is key in scripting.)

And it worked!

Thank you so much. I've spent so many hours last night looking for this fix.
Hopefully this post will help others with the same issue!

Thanks again! Really appreciate it!

wildbest

10:57 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



You are welcome.

Muji_san

12:07 am on Jan 17, 2009 (gmt 0)

10+ Year Member



Darn! I tested the wrong link.

your suggestion...

[I've suggested you use this code:
Redirect 301 /thispage.htm http://www.example.com/otherpage/otherpage.htm]

takes me to an error page that says the page does not exist.

I should add...
the reason I didn't add the htm in the first place was because the original designer of the webpages had a structure where he would name his file

index.htm and put it in a folder called 'thispage' then use
www.example.com/thispage to link to it.

which would direct him to the index page within the folder 'this page'

therefore i am using...

Redirect 301 /thispage http://www.example.com/otherpage/otherpage.htm

which takes me to this page

http://www.example.com/otherpage/otherpage.htm/

Which is great EXCEPT that it ADDS A TRAILING SLASH AT THE END OF THE ADDRESS that I do not want.

which then gives me a 404 error message.

If I can somehow remove the / (TRAILING SLASH) it would work.
but the .htaccess code somehow writes in that TRAILING SLASH automatically.

Hope this makes sense.

Muji_san

12:15 am on Jan 17, 2009 (gmt 0)

10+ Year Member



I figured it out!

I added a / (trailing slash) to the old address

Redirect 301 /thispage/ http://www.example.com/otherpage/otherpage.htm

instead of

Redirect 301 /thispage http://www.example.com/otherpage/otherpage.htm

and it works!

Muji_san

1:43 am on Jan 17, 2009 (gmt 0)

10+ Year Member



Not sure if I should start a new thread for this new problem..

My redirect is working now. Yes!

Now I would like to add more webpage redirects.

I tried just adding another line.

Redirect 301 /thispage/ http://www.example.com/otherpage/otherpage.htm
Redirect 301 /thispage2/ http://www.example.com/otherpage2/otherpage2.htm

This addition of the second redirect
leads to each of the page redirects to... "The server encountered an internal error or misconfiguration and was unable to complete your request."

Can't figure this one out.
Help please.

wildbest

10:23 am on Jan 17, 2009 (gmt 0)

10+ Year Member



Not sure what exactly you want to do and why you do it this way? Depending on answers you might need to consider:

- change misleading titles of your folders and pages to, say http://www.example.com/otherfolder/otherpage.htm or http://www.example.com/folder1/page1.htm or http://www.example.com/folder2/page2.htm
- You might need putting your access file in /thisfolder/ and /thisfolder2/ folders and not in the root.
- Instead of http://www.example.com/otherfolder/otherpage.htm why don't you just put all your files in the root folder like http://www.example.com/otherpage.htm?
- finally, if more complex redirect rules are needed you might want to use mod_alias RedirectMatch rules or mod_rewrite.

[httpd.apache.org...]

g1smd

10:32 am on Jan 17, 2009 (gmt 0)

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



If you have changed the URL of a page, you should also change the URL in the links pointing to those pages.

The redirect is for people who still have the old URL bookmarked, and for search engines who will keep coming back trying the old URL potentially forever.

For people clicking links within your site to go to other pages within your site, they should get directly to the page and should not pass through a redirect.

g1smd

10:37 am on Jan 17, 2009 (gmt 0)

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



Is /thispage/ a folder, an index page in a folder, or a page without an extension?

If it is either of the first two, what happens when you request /thispage/index.html?

Muji_san

11:27 am on Jan 17, 2009 (gmt 0)

10+ Year Member



The original designer created a /thispage/ folder and placed an index.htm file so that

www.example.com/thispage

would open the index page inside.

After lots of research and your nice comments I have decided that, though it may take a lot of time, It would be best to go back to the original address/naming convention. They have 10 years of links directed to the older links. I've only updated the names in the last couple of months.

It seems that for more than 30 redirects the 'redirect 301' method isn't the best one to use.
Thanks for your help.

g1smd

12:19 pm on Jan 17, 2009 (gmt 0)

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



Using a redirect is a good method - if you do it right.

If you are redirecting an index page URL, then you'll want to be more open about the syntax of the incoming URL requests that will work. You'll want to redirect for all of these:
/folder
/folder/
/folder/index.html
/folder?someparam
/folder/?someparam
/folder/index.html?someparam

as well as for URLs including port numbers and so on.

You'll also need it to redirect whether the original request was for www or for non-www URLs.

In that case a one line

RewriteRule [R=301,L]
would be useful.

.

If there is an "easy" relationship between all of the old names and all of the new names, one line of code could redirect for all 30 files/folders at the same time.

.

If Google has indexed any of your new URLs while you were experimenting, you might now need a redirect to tell them to go back to the old URL. Never test this stuff on a live site. Set up a Password protected "test" sub-domain where you can test code out first.

jdMorgan

7:32 pm on Jan 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may be able to solve your problem by using RedirectMatch:

RedirectMatch 301 /thispage(/.*)?$ http://www.example.com/otherpage/otherpage.htm
RedirectMatch 301 /thispage2(/.*)$ http://www.example.com/otherpage2/otherpage2.htm

However, if mod_dir is applying a DirectoryIndex rewrite or a trailing-slash-fixup redirect, or if MultiViews (content-negotiation) is getting involved, then you may need a more robust solution, such as:

Options +FollowSymLinks -MultiViews
RewriteEngine on
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /thispage/?[^\ ]*\ HTTP/
RewriteRule ^thispage/?$ http://www.example.com/otherpage/otherpage.htm [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /thispage2/?[^\ ]*\ HTTP/
RewriteRule ^thispage2/?$ http://www.example.com/otherpage/otherpage2.htm [R=301,L]

The RewriteConds in these rules require that the "thispage" URL-paths be directly requested by the client (browser, search engine robot, etc.), so that the rules will not be re-applied if the URL-path is subsequently rewritten *back* from the URL-path /otherpage/otherpage.htm to the filepath /thispage or /thispage/ as the result of the action of mod_dir, etc. As written, these redirects will be applied only if the client directly-requests "/thispage" or "/thispage/".

Jim