301 redirect entire subdirectory to a single page

Discussions about Apache mod Rewrite, .htaccess, Use, experiences ... URL Rewriting.

Moderator: Moderators

301 redirect entire subdirectory to a single page

Postby ebnx » Sat Nov 24, 2007 6:39 am

Folks:

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.website.com/sub/
To be 301 redirected to: -http://www.website.com/page.html

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

Redirect 301 /sub -http://www.website.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 in the past.

Thanks for your help!
ebnx
 
Posts: 21
Joined: Thu Nov 01, 2007 9:54 am

Advertisement

Postby SeO » Sat Nov 24, 2007 1:01 pm

You could use :

Code: Select all
ReWriteRule ^dir/.*$ /location-where-to-go.html [L,R=301]


Right after the www prefix redirection in case you're using it (which you should), eg before the phpBB rewriterules.
SeO
Admin
Admin
 
Posts: 6334
Joined: Wed Mar 15, 2006 9:41 pm

Postby ebnx » Sat Nov 24, 2007 9:25 pm

Thanks SeO!

Someone else had also suggested that I use the following in my .htaccess to achieve the same 301-redirect result:

Code: Select all
RedirectMatch 301 ^/sub(.*)$ http://www.website.com/page.html


Do you have any thoughts on whether one is more appropriate/effective than the other?

Thanks again!
ebnx
 
Posts: 21
Joined: Thu Nov 01, 2007 9:54 am

Postby Peter77 » Sat Nov 24, 2007 11:41 pm

I think the one SeO has given you is probably more efficient since it is using the L rule ( last rule ) so that it stops searching for more to rewrite -- it doesn't go any further then it needs too... so probably saving server resources.
Peter77
phpBB SEO Team
phpBB SEO Team
 
Posts: 532
Joined: Wed May 10, 2006 9:46 am

Postby ebnx » Sat Nov 24, 2007 11:54 pm

Thanks for that tip, Peter!
ebnx
 
Posts: 21
Joined: Thu Nov 01, 2007 9:54 am

Postby ebnx » Sun Nov 25, 2007 8:11 am

I'm encountering a strange issue with the 301 redirect.

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

-http://www.website.com/sub/

To redirect to a specific page:

-http://www.website.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.website.com/sub/example.php?id=section1

And using SeO's .htaccess directive, they are being redirected/rewritten as

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

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

Thanks very much!
ebnx
 
Posts: 21
Joined: Thu Nov 01, 2007 9:54 am

Postby dcz » Sun Nov 25, 2007 3:06 pm

What about :

Code: Select all
RedirectPermanent /sub http://www.example.com


Before :

Code: Select all
RewriteEngine On


If it's not enough to get rid of the _GET vars, it may be easier to deal with it in the target file directly.

++
Useful links :
SEO Forum || SEO Directory || SEO phpBB || Search
____________________

Liens Utiles :
Forum référencement || Annuaire référencement || Référencement phpBB || Recherche
dcz
Admin
Admin
 
Posts: 21383
Joined: Fri Apr 28, 2006 9:03 pm

Postby ebnx » Sun Nov 25, 2007 11:51 pm

I found a solution from another forum:
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.html? [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.


Thanks!
ebnx
 
Posts: 21
Joined: Thu Nov 01, 2007 9:54 am

Re: 301 redirect entire subdirectory to a single page

Postby Professional » Sun May 19, 2013 8:03 am

hi.
i recently moved from http://www.mobile4persian.net to forum.mobile4persian.net

i want to redirect all sub directory of http://www.mobile4persian.net/(all) to forum.mobile4persian.net .
what should i do?
انجمن تخصصی نسل جدید موبایل ها-Apple Portal
My Handwritings: Professional Dreams
Every Thing That U Feel,Is Every Thing That I Feel.
User avatar
Professional
PR5
PR5
 
Posts: 550
Joined: Mon Apr 07, 2008 5:41 am
Location: 1/2 of the World

Re: 301 redirect entire subdirectory to a single page

Postby HB » Mon May 20, 2013 6:04 pm

Isn't that the same as directing www to non-www? If so, there's plenty of how-tos. For example:

http://www.scriptalicious.com/blog/2009 ... -htaccess/
Dan Kehn
HB
phpBB SEO Team
phpBB SEO Team
 
Posts: 1509
Joined: Mon Oct 16, 2006 2:25 am


Return to Apache mod Rewrite

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 3 guests