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

Apache Web Server Forum

    
Redirect Dynamic URLs to Root
301 redirecting dynamic URL with query strings to root
pro_seo




msg:3986573
 11:50 am on Sep 9, 2009 (gmt 0)

I have a domain which had pages like http://www.example.com/page_name.aspx?id=56651

The website is now hosted on a Linux server.

I want to redirect all such pages with query strings to the root

This is the code I am trying to use

Rewrite Engine On
Options +FollowSymlinks

RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^$ http://www.example.com/? [R=301,L]

However, no luck till now.

Any help will be very appreciated.

Thanks!

 

jdMorgan




msg:3986785
 6:21 pm on Sep 9, 2009 (gmt 0)

Please define "no luck" -- I'm afraid that's quite meaningless, and I could ask many questions as a result:

Did you completely flush (delete) your browser cache before testing?

Where is your code located -- httpd.conf? .htaccess? If so, in which directory?

Is the "Rewrite Engine On" directive simply mis-typed above, or is that extra space actually present in your code?

Do you expect the RewriteRule pattern (which specifies that the local URL-path be entirely blank) to match a request for "page_name.aspx"?

Is "page_name" a literal (fixed, unchanging) string, or does it change? I can assume that it always ends with ".aspx", but what about the other part(s) of it?

As a matter mostly of "style," the Options directive should precede the "RewriteEngine on" directive.

As you can see, it's difficult to clearly communicate both the problem and any possible solutions. Concise but thorough is difficult to achieve...

Maybe this will get you closer:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^id=
RewriteRule \.aspx$ http://www.example.com/? [R=301,L]

Jim

pro_seo




msg:3987189
 7:30 am on Sep 10, 2009 (gmt 0)

Hello Jim - thanks for your response and sorry for not being clear enough with my first post. I really suck big time with HTACCESS coding :P

So here are the answers:

Did you completely flush (delete) your browser cache before testing?

Yes

Where is your code located -- httpd.conf? .htaccess? If so, in which directory?

.htaccess and in the root folder

Is the "Rewrite Engine On" directive simply mis-typed above, or is that extra space actually present in your code?

It's mistyped in the post - the code has "RewriteEngine On"

Do you expect the RewriteRule pattern (which specifies that the local URL-path be entirely blank) to match a request for "page_name.aspx"?

Not sure I understand this question - but I would expect it to match every instance of "page_name.aspx?id=" and redirect it to the root. So, no, I don't want it to match simply an empty URL path.

Is "page_name" a literal (fixed, unchanging) string, or does it change? I can assume that it always ends with ".aspx", but what about the other part(s) of it?

The page_name.aspx is a fixed string - only the query strings [ID]s vary.

As a matter mostly of "style," the Options directive should precede the "RewriteEngine on" directive.

Is this specific for this kind of redirects? Sorry, but I have been using Options +FollowSymlinks below the RewriteEngine On directive for so long. Can you kindly elucidate?

Btw, I tried out your code and it worked perfectly!. Can you please let me know where it went wrong so that I can know the mistakes I made.

Thanks a lot again Jim :)

jdMorgan




msg:3987330
 2:07 pm on Sep 10, 2009 (gmt 0)

The page_name.aspx is a fixed string - only the query strings [ID]s vary.

In that case, the code won't work perfectly, because any URL with an "id=" query string attached to it would match the rule. You'll be wanting:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^id=
RewriteRule ^page_name\.aspx$ http://www.example.com/? [R=301,L]

Is this specific for this kind of redirects? Sorry, but I have been using Options +FollowSymlinks below the RewriteEngine On directive for so long. Can you kindly elucidate?

As I said, it's a matter of 'style' -- The reason to put it just above RewriteEngine on is so that it's easy and quick to determine that you didn't forget to enable SymLinks, which is required for mod_rewrite to function. Therefore, I prefer to put it right before "RewriteEngine on" so that it's easy to spot. Simple as that.

Note that directives are processed in per-module order. That is, each Apache module in turn looks at your code and executes only those directives that it understands. Therefore, the "Options" directive will be executed before the "RewriteEngine on" directive, regardless of which you place first in your code; You control the execution order of directives only to a limited extent -- Only those directives handled by any given single module will execute strictly in the sequence that you specify. The order of module execution is determined by the reverse order of the LoadModule list in Apache 1.x, and by an internal priority scheme in Apache 2.x.

Jim

pro_seo




msg:3987578
 8:52 pm on Sep 10, 2009 (gmt 0)

Thanks a lot Jim for not only providing the correct code but also for taking the time to explain the directive execution order in details.

Very much appreciated indeed :)

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