Forum Moderators: phranque
RewriteCond %{QUERY_STRING} .
RewriteRule ^$ http://www.example.com/? [R=301,L]
This works fine to redirect query strings on my main page. Is there a way to write it so query strings on all pages redirect?
Ideally I'd like to 301
www.example.com/dir1/?ahahha
to
www.example.com/dir1/
but if I could redirect all query strings simply to the index page, that would be okay too. (I don't use query strings for anything, so I don't really care what page people would end up at.)
Thanks.
RewriteRule ^(.*)$ http://www.example.com/$1? [R=301,L]
-- OR --
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
Question 1: Is there a way to use the code above, but allow query strings for a specific directory or even a specific URL?
Question 2: I tried this...
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/dir1/$1? [R=301,L]
but it didn't make
http://www.example.com/dir1/?blah
redirect to
http://www.example.com/dir1/
Is there a way to do this for a single internal page? Or (better) all pages within one directory?
If that's not what you wanted, then please be specific (no multiple choices)... The code has to be explicit.
Also, the intent of this forum is to encourage you to ask questions, experiment, and learn. The resources cited in our forum charter and the threads in the Apache section of the WebmasterWorld library may prove helpful.
Jim
I only had
RewriteCond $1 ^dir1/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
Should I have had...
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
RewriteCond $1 ^dir1/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
Using the snippet I posted, as written, all URLs pointing to example.com/dir1/ with query strings should be redirected to remove the query string. So it may be that the code is in the wrong location.
I'm also assuming you've flushed your browser cache after each change to the code, and that no other rewriterule preceding this one --in the same .htaccess file or in a higher-level .htaccess file-- is interfering by rewriting these URLs before the new code can be executed.
Jim
The level above it. I have no htaccess file in /dir1/
Should I put an htaccess file in /dir1/ and add only those three lines above to it?
"...and that no other rewriterule preceding this one --in the same .htaccess file or in a higher-level .htaccess file-- is interfering by rewriting these URLs before the new code can be executed...."
I got a bunch, this is what I have now in the root level:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^123.45.678.90 [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} .
RewriteRule ^$ http://www.example.com/? [R=301,L]
All these are basically more important than this new thing I want to do. But can I add a new htaccess to /dir1/ that only says...
RewriteCond $1 ^dir1/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
This would be a great solution for me since I could just pick specific directories to add it too, but leave everything else alone.
Should you choose to try it in the dir1/ subdirectory, the code needs to change to reflect its new location:
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com[b]/dir1/[/b]$1? [R=301,L]
Jim
[edited by: jdMorgan at 1:10 am (utc) on Jan. 1, 2007]
RewriteCond %{QUERY_STRING} .
RewriteRule ^$ http://www.example.com/? [R=301,L]
with
RewriteCond $1 ^dir1/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
instead of adding it beneath it.
So now if I wanted to do this with multiple directories, I could add that other code in an htaccess file in each sub directory, or in the main htaccess can I do...
RewriteCond $1 ^dir1/
RewriteCond $1 ^dir2/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
Or would I need to do something else?
RewriteCond $1 ^dir1/ [OR]
RewriteCond $1 ^dir2/ [OR]
RewriteCond $1 ^dir3/ [OR]
RewriteCond $1 ^dir4/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
RewriteCond $1 ^(dir1¦dir2¦dir3¦dir4)/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
Note in the first example that there is no [OR] on the last "dir" condition, and this is important.
Replace the broken pipe "¦" characters in the second example with solid pipes before use; Posting on this forum modifies the pipe characters.
Jim