Forum Moderators: phranque
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]
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.
RewriteEngine on
#
RewriteCond %{QUERY_STRING} cat=([0-9]+)
RewriteRule ^category.php$ [domain.com...] [R=301,L]
Doesn't seem to be working just yet.
In other words, there is nothing at all wrong with a Lamborghini, unless a dump truck is what is required...
Jim
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.
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.
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)
RewriteEngine on
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=[0-9]+
RewriteRule ^category\.php$ http://www.example.com/newfolder/? [R=301,L]
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?
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 URLsHowever, 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].
Jim
[edited by: jdMorgan at 1:48 pm (utc) on Mar. 12, 2009]
/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?
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?
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.
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.