Forum Moderators: phranque

Message Too Old, No Replies

Can you run a mod rewrite & 301 redirect at same time?

         

kevgibbo

7:07 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



I'm currently trying to run a mod_rewrite to display the content from an old URL to a new version. This works fine but I'm also trying to 301 redirect the old page to the new one, which isn't working.

I've read quite a few apache guides but I'm still having problems, any idea what's wrong with this code?

RewriteEngine on
RewriteRule ^category.php?cat=1$ [domain.com...] [R=301,L]

g1smd

7:16 pm on Mar 10, 2009 (gmt 0)

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



Yes. There are several current threads in this forum, posted to today discussing this.

You need two things: a redirect for direct client requests, and a rewrite.

The redirect and the rewrite must each be coded using a RewriteRule.

The redirect should contain the domain name in the target URL, and [R=301,L].

Be aware that RewriteRule cannot see query string data. You need an additional RewriteCond to look at that.

kevgibbo

7:42 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



Thanks a lot for your help, does this mean I should be doing something like:

RewriteEngine on
#
RewriteCond %{QUERY_STRING} cat=([0-9]+)
RewriteRule ^category.php$ [domain.com...] [R=301,L]

Doesn't seem to be working just yet.

jdMorgan

11:16 pm on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would be most helpful if you would tell us what you want to do in terms of the *exact* old URL, and the *exact* new URL. You have provided code and asked what's wrong with it, but you did not say how you tested it, what results you wanted, what result you got, or how the actual results differed from those expected.

In other words, there is nothing at all wrong with a Lamborghini, unless a dump truck is what is required...

Jim

kevgibbo

8:13 am on Mar 11, 2009 (gmt 0)

10+ Year Member



Thanks Jim, I'm trying to setup a mod_rewrite so that /newfolder displays the content of /category.php?cat=1

I've done this successfully by using the code I posted in the first message, but when visiting /category.php?cat=1 directly this is still displaying the same content as before. What I'm trying to do is 301 redirect this to /newfolder instead.

The latest code I tried from other posts on this forum didn't display /category.php?cat=1 content on /newfolder. Instead this displayed the homepage content on all 404 error pages. Plus the redirect didn't work from old page to new either.

g1smd

9:38 am on Mar 11, 2009 (gmt 0)

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



You'll need a redirect (using a RewriteRule) placed before the rewrite. The code you have is almost correct. What is the % in the rule for? Is that meant to be a reference to %1 here? The redirect will contain the full domain name and [R=301,L] as you have done. You will will need a RewriteCond to examine the QUERY_STRING paired with the RewriteRule for the redirect. That bit captures the number. That part looks OK.

You'll also need an internal rewrite that connects URL requests for /newfolder to the internal filepath where the content resides. That rewrite will NOT contain a domain name, and must be placed after the redirect code. I don't see such a rewrite coded in this thread yet.

Flush your browser cache before testing otherwise you'll be pulling content from your cache not checking the actual server response.

kevgibbo

10:10 am on Mar 11, 2009 (gmt 0)

10+ Year Member



Thanks a lot for your help with this, I think it's getting closer - although still not quite there yet.

I've just tried the following code, removing the % as this wasn't needed and dropping the domain:

RewriteEngine on
#
RewriteCond category.php%{QUERY_STRING} cat=([0-9]+)
RewriteRule ^category.php$ /newfolder [R=301,L]

The results are that /newfolder is now displaying a 404 error and accessing /category.php?cat=1 redirects to /newfolder?cat=1 (which is also a 404)

g1smd

7:30 pm on Mar 11, 2009 (gmt 0)

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



You need to have the domain name and [R=301,L] within the redirect.

For the rewrite, you must omit the domain name, and end with just [L].

The redirect works in the opposite direction to the rewrite.

Can you show both of these together in your next post, with the redirect listed first.

jdMorgan

7:54 pm on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In order to prevent a looping problem, the redirect must only be invoked for direct client requests of the dynamic URL. Fixing that and the problematic syntax of the code above, we get:

RewriteEngine on
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=[0-9]+
RewriteRule ^category\.php$ http://www.example.com/newfolder/? [R=301,L]

Jim

kevgibbo

11:46 am on Mar 12, 2009 (gmt 0)

10+ Year Member



Thanks again for your help with this, I'm still having a couple of problems though.

I've tried the code added by JdMorgan. This redirected the old URL to /newfolder successfully, but the new URL was a 404 error rather than reflecting the content of the old page.

Do I need to use /newfolder within the RewriteCond so that this grabs the content from the old page?

jdMorgan

1:38 pm on Mar 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to define --for yourself and for us-- what *filepath* (not URL) is the actual location of the content. URLs and filepaths are *not* the same thing: URLs are used on the Web, filepaths are used inside the server, and they are not equivalent, they are only "associated." Using mod_rewrite changes this association.

I have been proceeding under the assumption that /newfolder is a physically-existing directory-index page. If this is not true, then please clarify, as there is nothing in this thread that states otherwise.

If you are only trying to change the URL, then because it seems to be a major point of confusion for many Webmasters, I'll reiterate something I just posted in another thread:

If you want to change your URLs, then you must do so -- by changing the URLs in the links that you publish on your Web pages. Your pages *define* the links for all Web clients; Once your dynamic links have been published, it's too late for any server-side incoming-request-handling code to do anything about it -- Users and search spiders have already seen them.

Further, redirecting every single incoming request for these dynamic URLs to their static equivalents would result in two client HTTP requests for each page loaded, slowing down the "user experience" and trashing your "site stats."

The short version of the correct procedure is to:
1) Change your on-page links to SEO-friendly static form
2) Use an internal rewrite rule to rewrite incoming requests for "friendly" URLs to the proper script filepath+query
3) Optionally, externally redirect incoming client requests (only) for the old dynamic URLs to the new static URLs

However, optional step 3 is a fix-up to speed search engines' re-indexing of your URLs, to preserve the functionality of old bookmarks and old links from sites that you don't control, and to prevent duplicate-content problems in search ranking. It cannot practically be used without doing the other two steps first.

...

For more information on this subject, see Changing Dynamic URLs to Static URLs [webmasterworld.com] in our Apache Forum Library [webmasterworld.com].


What we have been discussing here is only step three, and there's a good chance that steps 1 and 2 haven't been done yet.

Jim

[edited by: jdMorgan at 1:48 pm (utc) on Mar. 12, 2009]

kevgibbo

5:36 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



Agree about changing all active links to new versions.

/newfolder is the new URL which is written, but this does not exist as a physical folder on the server.

We've also tried the file path, both with and without /public_html

But we do seem to be making progress:
Options +FollowSymLinks
RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php
RewriteRule ^category\.php$ /newfolder/ [R=301,L]

RewriteRule ^newfolder/?$ /category.php?cat=1 [L]

The above code redirects /category.php?cat=1 to /newfolder/?cat=1 and displays the correct content (ideally this should drop cat=1 from the URL but not major issue).

The main problem is that there are sub categories which are now displaying the same content as cat=1 rather than their own content (cat=1&sub_cat=2).

The redirects here seem ok, although it would be good to have more control over inserting keywords:
/category.php?cat=1&sub_cat=2
redirects to:
/newfolder/?cat=1&sub_cat=2

But the content is displayed from the cat=1 page instead of ?cat=1&sub_cat=2. Hope that all made sense?

g1smd

8:11 pm on Mar 12, 2009 (gmt 0)

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



Check my comments above again. You need to specify the domain name in the redirect. You did have that right, right at the beginning.

You can suppress the parameter from being re-appended by adding a question mark to the end of the target URL of the redirect.

You need to look at the parameter values within THE_REQUEST to match the appropriate pattern.

.

The rewrite looks OK, but do you need this rewrite for just one URL, or for multiple URLs?

If multiple URLs, you can use patterns and back-references to capture and re-use the values.

.

You didn't mention sub-categories before, so those have not been taken into account in any code already posted.

The biggest part of this job is defining exactly what you want to happen for any and all formats of URL that could possibly be thrown at the server. Once you know exactly which URL formats are affected and what needs to happen to those requests, the coding part is often fairly trivial.

One thing... you mention

/category.php?cat=1&sub_cat=2
but what would happen if someone requests
/category.php?sub_cat=2&cat=1
from the server? It is after all, a completely valid request. What about
/category.php?sub_cat=2&cat=1[b]&[/b]
too?

nektotigra

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

10+ Year Member



Hi there,
I have a similar problem with my category paging plug-in for the NucleusCMS.

When a user browses categories from the first page, the url looks like [domain.tld...] and the second page looks like [domain.tld...] , which is alright with me. However, when the user returns to the first page of the category, it starts to look as [domain.tld...]
Is there a safe way to remove the "?page=1" part with .htaccess file?
Thanks.

g1smd

4:51 pm on Jun 13, 2009 (gmt 0)

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



The best way would be to remove it from the link that the user actually clicks on. That is where the URL is generated.

However, you can also set up a redirect that redirects requests for one URL to the other URL format. You'd need a RewriteCond that looks at QUERY_STRING and the RewriteRule with [R=310,L] to do the work.

If you don't fix the links that people actually click on, then every user will be taken through this redirect when they click on the navigation links within your site. This clutters your log as each page uses two HTTP requests instead of one, skews your stats and slows down user access.

The people that designed the CMS need to be fixing this in their code as the product is SEO deficient.