Forum Moderators: phranque
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!
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]
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 :)
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