homepage Welcome to WebmasterWorld Guest from 54.226.168.96
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

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

Apache Web Server Forum

    
301 redirect entire subdirectory to a single page
How do I do this in .htaccess?
eager2learnSEO




msg:3512295
 6:41 am on Nov 24, 2007 (gmt 0)

I'm interested in 301 redirecting all pages in a subdirectory of my website to a single page off of the root directory. To illustrate:

I want all pages in: http://www.example.com/sub/
To be 301 redirected to: http://www.example.com/page.html

I thought I could do it by adding the following command to my .htaccess file:

Redirect 301 /sub http://www.example.com/page.html

But it doesn't seem to be working. Anyone have any advice on what I should be putting into my .htaccess?

The RewriteEngine is turned on, and I've been able to 301 redirect other pages (one page to another page) in the past.

Thanks for your help!

[edited by: jdMorgan at 3:58 pm (utc) on Nov. 24, 2007]
[edit reason] example.com [/edit]

 

jdMorgan




msg:3512492
 3:58 pm on Nov 24, 2007 (gmt 0)

The Redirect directive, as documented, takes the URL-path prefix on the left, and redirects to the URL-prefix of the right, appending any path information not explicitly included in the left-side URL-path prefix to the URL-path on the right. So, it is behaving exactly as described in the documentation.

What you need is the RedirectMatch directive, which can be told to 'drop' path information:

RedirectMatch 301 ^/sub(.*)$ http://www.example.com/page.html

The RewriteEngine directive controls whether RewriteRules in mod_rewrite are enabled, and has nothing to do with either Redirect or RedirectMatch directives, both of which are part of mod_alias.

Jim

eager2learnSEO




msg:3512661
 9:23 pm on Nov 24, 2007 (gmt 0)

Thanks jdMorgan for your reply. I was also suggested this .htaccess command by another person:

ReWriteRule ^sub/.*$ /page.html [L,R=301]

Do you have any opinion as whether one is more appropriate than the other?

Thanks again!

eager2learnSEO




msg:3512883
 8:13 am on Nov 25, 2007 (gmt 0)

I'm encountering a strange issue with the 301 redirect command--I'm wondering whether you can provide me some suggestions?

As I said before, I wanted all pages in a specific subdirectory:

http://www.example.com/sub/

To redirect to a specific page:

http://www.example.com/page.html

The strange thing that is happening is that several of the pages in the subdirectory have weird php URLs like:

http://www.example.com/sub/example.php?id=section1

And using your suggested .htaccess directive, they are being redirected/rewritten as

http://www.example.com/page.html?id=section

Anyone have any thoughts on how I can get rid of the "?id=section1" part?

Thanks very much!

jdMorgan




msg:3512959
 2:09 pm on Nov 25, 2007 (gmt 0)

This would likely be a code-order problem, where the internal rewrite of the static URL to the 'weird' dynamic URL is occurring before the external redirect.

In order to control the order of execution you will need to use the mod_rewrite version of the redirect, and place the redirect ahead of the internal rewrite. Further, you'll want to clear any query string received with the client request:

RewriteRule ^sub/ /page.htm[b]l?[/b] [R=301,L]

I assume that you already have other working RewriteRules. If not, you will need to 'set up' mod_rewrite by putting the following code before your first RewriteCond or RewriteRule:

Options +FollowSymLinks
RewriteEngine on

The Options directive may not be allowed on your server, but if it is allowed, it may be required. There is no way to tell except to try it. If it is not allowed and also required, then you won't be able to use mod_rewrite on your server.

Jim

eager2learnSEO




msg:3513236
 11:49 pm on Nov 25, 2007 (gmt 0)

That worked perfectly, thank you!

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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved