homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Can I 301 this type of a link?

 1:05 pm on Mar 12, 2010 (gmt 0)

I can't seem to redirect this link, its getting found in the serps from a old site and I want to 301 it.

The link is as follows:


[edited by: jdMorgan at 1:19 am (utc) on Mar 13, 2010]
[edit reason] example.com [/edit]



 9:54 pm on Mar 12, 2010 (gmt 0)

Test the QUERY_STRING using a RewriteCond and use a RewriteRule to do the redirect. Add [R=301,L] flags to finish off.


 5:24 am on Mar 13, 2010 (gmt 0)

We tried to 301 it, yet it doesnt go to the homepage.

What condition do you suggest?


 8:00 am on Mar 13, 2010 (gmt 0)

What code did you use?


 7:52 am on Mar 15, 2010 (gmt 0)

Hey g1smd, i tried using this i found on another forum:
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ http://example.com/page/%1.pdf [R=302,L]

..but have no idea how implement it with this URL so it goes to the homepage:


[edited by: jdMorgan at 1:35 pm (utc) on Mar 21, 2010]
[edit reason] example.com, obscured specifics. [/edit]


 7:56 am on Mar 15, 2010 (gmt 0)

Can you give us an example of the .htaccess code for testing the QUERY_STRING using a RewriteCond and to redirect to homepage


 8:00 am on Mar 15, 2010 (gmt 0)

You just posted code containing a RewriteCond that tests the QUERY_STRING value.

Change the redirect to be a 301 not a 302.

Change the destination URL to be http://www.example.com/ instead of an individual PDF file.

Delete the first RewriteCond as you don't have a page with that name.

Change the query string pattern to match what you want to match, and delete the end anchor. The pattern is likely
&?p=2006&? or somesuch.

 8:54 am on Mar 15, 2010 (gmt 0)

Ok i've tried it with this code:

RewriteCond %{QUERY_STRING} ^p=2006&s=may&t=room-joins-gaming
RewriteRule ^(.*)$ [http://www.example.com/] [R=301,L]

..but it's making an infinite loop.

I think it might be because i've created another rule that makes this url: http://www.example.com/index.php
go to this url: http://www.example.com/, but it still uses the index.php for the homepage.

The problem is the querystring uses the index.php although it's not displaying it in the address bar.
I hope you understand what i meen.
Is there any other solution or am i doing this wrong ?

[edited by: jdMorgan at 1:36 pm (utc) on Mar 21, 2010]
[edit reason] example.com [/edit]


 11:39 am on Mar 15, 2010 (gmt 0)

Add a question mark to the end of the target URL if you want to clear the query string value, or else make the .* pattern more precise as to the path it matches.

At present, the pattern re-matches the new URL requested after the redirect.

To be sure there are no other errors, we need to see all of the redirect and rewrite rules.


 12:25 pm on Mar 15, 2010 (gmt 0)

i've tried doing this:

RewriteCond %{QUERY_STRING} ^p=2006&s=may&t=keyword-keyword-keyword
RewriteRule ^(.*)$ [http://www.getoff~mysite.website/?] [R=301,L]


RewriteCond %{QUERY_STRING} ^p=2006&s=may&t=keyword-keyword-keyword
RewriteRule ^(index.php)$ [http://www.getoff~mysite.website/] [R=301,L]

and this..

RewriteCond %{QUERY_STRING} ^p=2006&s=may&t=keyword-keyword-keyword
RewriteRule ^(.*)$ [http://www.getoff~mysite.website/index.php] [R=301,L]

..all giving a 404: not found error

Other rules present FYI:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php

..some simple redirects:
Redirect 301 /cgi-bin/top100/in.cgi http://www.getoff~mysite.website/
Redirect 301 /keyword1-keyword2-keyword3.php http://www.getoff~mysite.website/

..and this:
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]

[edited by: jdMorgan at 1:37 pm (utc) on Mar 21, 2010]
[edit reason] de-linked URLs [/edit]


 12:28 pm on Mar 15, 2010 (gmt 0)

Convert all rules using Redirect directive to instead use RewriteRule.

List all redirects before any of the rewrites. Redirects affect URLs.

F implies L. Omit L.

In your list of patterns, the /[^.]* pattern allows for / so you can delete the single / from the list of patterns. Rearrange the list so that the most often matched items are first.

Place your -f and -d checks after the other two conditions (the ones that currently follow) for efficiency.

Finally, I do want to be clear that you do not have [ and ] around the URL?

Use example.com to stop the forum auto-formatting any URLs posted in code.


 9:43 am on Mar 16, 2010 (gmt 0)

1. What do you mean by Redirect directive to instead use rewriterule ? Must the Redirect 301's be changed to RewriteRule ?

2. Ok i've moved all redirect 301's to top of all rewrite rules.

3. Ok i've left out L

4. Not sure what you mean here. I've rearranged the list so most often matched items are first.

5. -f and -d are now after the other two conditions

6. Noted ur comment about auto-formatted url's


 3:56 pm on Mar 16, 2010 (gmt 0)

1. Yes. Otherwise, you do not control whether mod_alias Redirect or mod_rewrite RewriteRule directives will execute first. Processing of your "code" is on a per-module basis, with each Apache module 'reading' your code and executing only the directives that it understands. Therefore, your code is *not* executed in strict line-by-line order, but rather, only line-by-line as processed by each Apache module in turn.

Therefore, mixing Redirect, RedirectMatch, and RewriteRule directives means that your code may not work as expected. Or worse, it may work as expected now, but suddenly stop working properly (and damage your search rankings) when you server is upgraded or when you change hosts. This can happen when an internally rewritten *filepath* (which is not supposed to be visible to client browsers or search engines) gets "exposed" as a URL by a subsequent external redirect due to incorrect rule order or unexpected/suddenly-changed module-processing order.

The above posts make it clear that you're having a bit of a hard time. But one thing is for sure: When this thread is concluded, you will have much better code, and a much better understanding/appreciation of mod_rewrite. :)

Keep up the good work, and by all means, refer to the mod_rewrite documentation at apache.org if posted comments and/or suggestion are not clear.



 9:30 am on Mar 17, 2010 (gmt 0)

ok, i will get into "changing the Redirect 301's to RewriteRule" soon

In the meantime, i need a quick solution for this type of url:
It must redirect to http://www.example.com/
Is it possible to do this the quick way?
Our rankings are being affected and I need the required code to fix this issue urgently.


 10:37 am on Mar 17, 2010 (gmt 0)

The suggestions above should have fixed the issues.

Post the code with ALL previously described changes and fixes applied.


 11:56 am on Mar 17, 2010 (gmt 0)

ok in that case, i'm still busy doing changes to 100+ Redirect 301's,

I'm having problems changing all the Redirect 301's to RewriteRules eg.

Redirect 301 /afolder/keyword100/in.cgi http://www.example.com/
RewriteRule ^(afolder/keyword100/in.cgi)$ [http://www.example.com/] [R=301,L]

this rule returned the following:

i'm also having trouble with url's with %20 (space) in the url's eg.
Redirect 301 "/keyword1 backup.htm" "http://www.example.com/" (this one worked)
RewriteRule ^(keyword1 backup.htm)$ [http://www.example.com/] [R=301,L] (this one returned an internal server error)

Please advise me with RewriteRules and %20's as well

This might be way above me, but giving up just isn't an option.


 7:02 pm on Mar 17, 2010 (gmt 0)

Get rid of the "[" and "]" symbols around your redirect-to URLs. They appear on this forum whenver you don't use "www.example.com" as the domain name or "break" the URLs so they don't auto-link, but are just artifacts of the forum.

Do yourself a favor and comment-out the great majority of those pre-existing Redirect directives for initial testing. Minimize the amount of code that you are changing and will have to test. Get it working one small step at a time to preserve sanity and promote efficiency.

Looking at your last example, I'd suggest:

RewriteCond %{QUERY_STRING} mosConfig_[A-Z_]{1,21}(=|\%3D) [NC,OR]
RewriteCond %{QUERY_STRING} base64_encode[^(]*\( [NC,OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} (GLOBALS|_REQUEST)(=|\[|\%[0-9A-Z]{0,2}) [NC]
RewriteRule ^ - [F]
RewriteCond %{QUERY_STRING} ^p=2006&s=may&t=keyword-keyword-keyword
RewriteRule ^(index\.php)?$ http://www.getoff~mysite.website/index.php? [R=302,L]
RewriteRule ^cgi-bin/top100/in\.cgi$ http://www.getoff~mysite.website/index.php? [R=302,L]
RewriteRule ^keyword1-keyword2-keyword3\.php$ http://www.getoff~mysite.website/index.php? [R=302,L]
RewriteRule ^keyword1\ backup\.htm$ http://www.example.com/ [R=301,L]
RewriteCond %{REQUEST_URI} (\.(php|html?|feed|pdf|raw)|/[^.]*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

All changes to patterns, substitutions, rule-set order, rewritecond order, etc. were intentional. Several redundancies were removed. Operation should either be identical to the original or more-correct, more-efficient, or both.

On rule order: Put your access controls first (rules using [F] flag), followed by all external redirects (rules with [R=30x] flags) in order from most-specific (single URLs affected) to least-specific (many URLs affected). Finish up with your internal rewrites, again in order from most-specific to least-specific.

Note that the RewriteConds in your last rule above --the internal rewrite-- have been re-arranged to prevent unnecessary checking of the disk for existing files and directories. This change alone should speed up your site -- perhaps noticeably under heavy traffic conditions. Several redundancies in the RewriteConds and patterns were eliminated as well.



 10:43 am on Mar 19, 2010 (gmt 0)

Hey, as i was doing all these changes to the htaccess file, i found that i couldn't log into the joomla backend anymore, so i had to revert back to the backed up file, i'm still busy trying to implement this in a way that works with joomla and for the redirects
Rome wasn't built in a day right


 10:45 am on Mar 19, 2010 (gmt 0)

maybe i should have mentioned earlier that this site is on a joomla platform and it's making the task a little bit harder


 9:59 pm on Mar 20, 2010 (gmt 0)

You need to exclude the joomla filepaths from some of the rules above, so that they will not interfere with "logging into the joomla back-end." Also, please understand that many readers here may not know anything about joomla, or the URLs that it uses. Therefore, we can't suggest what those exclusions might look like, except to say that they will be based on the URL-paths used to access this "back-end."


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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved