homepage Welcome to WebmasterWorld Guest from 54.205.189.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
301 redirect adding trailing slash and remove extension
philip




msg:4571868
 2:50 pm on May 8, 2013 (gmt 0)

Hello,

I am moving a website to a new domain, and i am having trouble writing the 301 redirect.

The following url patterns need to be redirected

1)
http://old.com/single.html
->
http://new.com/single/

2)
http://old.com/category
->
http://new.com/category/

3)
http://old.com/
->
http://new.com/


I think I have got the right code for #1 and #3 (pls check)

1) RedirectMatch 301 (.*)\.html$ http://new.com$1/

3) Redirect 301 / http://new.com/


But I donít know how to add a trailing slash for the category links #2.

hope you guys can help me out,

thx, phil

 

Dideved




msg:4571960
 6:07 pm on May 8, 2013 (gmt 0)

Your first two redirects seem perfectly fine.

Can we assume that a category will only ever be exactly one path segment? If so, then this seems to do the trick.

RedirectMatch ^(/[^/]+)$ http://new.com$1/

This will need to go between your two current redirects.

lucy24




msg:4572003
 7:42 pm on May 8, 2013 (gmt 0)

http://old.com/category

This is an URL, not a physical file. To make sure the redirect behaves properly, you need to know whether it represents a directory with missing final slash (on the original domain this would be handled by mod_dir) or an extensionless URL.

As your redirects are currently written, case #2 is covered by the rule intended for case #3. Remember that mod_alias (Redirect by that name) reappends the entire rest of the path.

If the requests represent missing directory slashes, they will then get a further redirect on arrival. You may choose to handle them manually if there's been a mistake and you're getting a fair number of requests in this form. But you're not obliged to do anything more about them. There's a longing list of Redirects You Don't Need Unless You Need Them; missing directory slashes are on the list.

If the requests represent extensionless URLs, you'll need more work.

Note that if your file and directory names contain no literal periods-- the hostname and the extension delimiter don't count --you can make the rules simpler and more efficient by using the forms
^[^.]$
and
^[^.]+/$
The second form potentially creates a single server hiccup, but it only involves one character.



Wait, let me save time by responding in advance to the follow-up post: BECAUSE A SINGLE OPTIMIZATION FROM THE SITE OWNER RESULTS IN THOUSANDS OF OPTIMIZATIONS FOR THE SERVER.

Dideved




msg:4572018
 7:59 pm on May 8, 2013 (gmt 0)

> ^[^.]$

That is neither simpler, nor more efficient, nor will it correctly match all URLs.

philip




msg:4573020
 10:15 am on May 11, 2013 (gmt 0)

i tried
RedirectMatch ^(/[^/]+)$ http://new.com$1/
but it did not add the trailing slash according to webconfs.com/http-header-check.php

i ended up using just rule #1 and #3 and every single url found its way to the new site. the category slash was added on the receiving end.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved