Forum Moderators: phranque

Message Too Old, No Replies

htaccess wildcard redirect

how to redirect several files in the root to a subfolder

         

bigwebidea

9:30 pm on Jun 23, 2009 (gmt 0)

10+ Year Member



If there a way to 301 redirect several files that are in the root to a subfolder. ie.

www.domain.com/12345.html to
www.domain.com/folder/12345.html

There are too many to do one at a time. Is there a way to make it work with a wildcard? ie.

www.domain.com/*.html to
www.domain.com/folder/*.html

g1smd

12:54 am on Jun 24, 2009 (gmt 0)

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



Yes. There's several thousand prior examples of similar code in this forum.

However before we get started, some questions:

Have you physically moved some files from one folder to another?

Having moved some files around inside the server, are you aware that you could still use the old URLs to access those files if you wanted to, by using a rewrite?

That is, is a redirect actually necessary?

Is this a similar issue? [webmasterworld.com...]

bigwebidea

2:16 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



Actually these are static html files, I'm moving them to a different server (same Apache configuration) and while doing so wanted to change the url structure from:

www.domain.com/12345.html (current server) to
www.domain.com/folder/12345.html (new server)

With these changes I want to let the search engines know of my change, usually done by htaccess 301 redirects. There are thousands of files and need some sort wildcard solution.

Thanks for your help...

jdMorgan

3:28 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you change the URLs, then you will lose all page ranking power from old links, unless you put in place redirects from the old URL to the new. If you do that, then the loss will only be temporary --from weeks to months (possibly six months or so)-- until the search engines completely re-index your site.

So what g1smd is telling you is that moving files around inside a server *does not* always require the URL to be changed, and that you can move the files to a subfolder and simply rewrite requests for their previous URLs to the new fiepath. Note: A URL is not a filepath -- They are only "associated" things.

In this case it might be a simple matter to internally rewrite requests for any URL like /<numbers>.html to the filepath /folder/<numbers>.html, and leave the URLs alone.

If your new server has a different domain, then it would also be fairly simple to redirect from the old server's domain to the new while retaining the original URL-path, and then rewrite those original URL-paths to the new filepath inside the new server. However, much of the SEO value of retaining old URLs will be lost if you change domains as well as servers.

If you decide to proceed with external redirection rather than internal rewriting, we have lots of previous threads here on redirecting to a subfolder while avoiding 'infinite' redirection loops -- Try the site search link at the top of this page. This is also a trivial redirect, and the examples in the Apache mod_rewrite --or mod_alias-- documentation should be sufficient to get you started.

This all may seem pedantic, but it does you no good if we give you perfectly-good technical redirect advice but don't mention that your site will take a hit in search ranking... temporarily, at least.

Jim

bigwebidea

8:07 pm on Jun 25, 2009 (gmt 0)

10+ Year Member



Ok, that's making sense now. Big thanks for your advice. Some research has brought me to the below, but this is giving me a 500 server error.

RewriteEngine On
RewriteRule ([^.]+)\.htm$ /folder/$1.htm

I'll continue to play...

g1smd

12:18 am on Jun 26, 2009 (gmt 0)

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



That's a rewrite.

If you ask for the URL example.com/something.htm the server will try to get content from /folder/something.htm

Problem is, with /folder/something.htm this now matches the rule again, and the server will try to get content from /folder/folder/something.htm - but this now also matches the rule again.

So, you need to add [L] to the end of your rule. You also need a better pattern that does not keep matching recursively. Currently it matches "not a period". I would change [^.] to [^./] instead.

If you want a redirect (instead of a rewrite), you need to also add the domain name and [R=301,L] to the target. A redirect forces the browser to request a new URL

Is that what you want. Be aware there are at least three other threads active yesterday and today covering the same ground.

bigwebidea

6:15 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



This seems to work for me. Do you see any issues? Thanks for your help on this!

# Internally rewrite requests for /<page>.htm to /folder/<page>.htm
RewriteRule ^([^./]+)\.htm$ /folder/$1.htm [L]

g1smd

7:30 pm on Jun 26, 2009 (gmt 0)

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



So, if page in root folder is requested, it fetches page from /folder/ instead.

Looks OK. The other way to do it is to add a preceding RewriteCond looking at THE_REQUEST so it only matches Direct Client Requests.

bigwebidea

9:47 pm on Jul 15, 2009 (gmt 0)

10+ Year Member



# Internally rewrite requests for /<page>.htm to /folder/<page>.htm
RewriteRule ^([^./]+)\.htm$ /folder/$1.htm [L]

This worked well, but will I not be hit for duplicate content?

www.domain.com/12345.html
www.domain.com/folder/12345.html

jdMorgan

12:49 am on Jul 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, possibly, if there are links to the "real filepath." If so, those should be corrected, and then a second rule will redirect any erroneous incoming links not under your control:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /folder/[^.]+\.htm\ HTTP/
RewriteCond ^folder/([^.]+\.htm)$ http://www.example.com/$1 [R=301,L]

Note that the RewriteCond requires that the 'real filepath' URL be requested directly by a client to invoke this redirect; Internal requests invoked by your rewrite won't be redirected, so no 'infinite loop' will result.

Jim