Breaking my head on this one..
Need to check if a get variable named 'filter' has value or not with mod_rewrite..
http://site.com/audio/speakers?filter= should redirect to: http://site.com/audio/speakers
http://site.com/audio/speakers?filter=yes shouldn't redirect..
Thanks!
You can try this:
RewriteCond %{QUERY_STRING} ^filter=$ [NC]
RewriteRule ^audio/speakers$ http://site.com/audio/speakers? [R=302,L]
It will only check for this URL: /audio/speakers?filter= (where filter is first and only one parameter and is empty) -- exactly like in your URL example. If there will be more than one parameter .. it will not match and will not do anything (even if filter will be empty).
You can change redirect code form 302 (temp) to 301 (permanent) if required.
UPDATE:
RewriteCond %{QUERY_STRING} ^filter=$ [NC]
RewriteRule ^(.*)$ http://site.com/$1? [R=302,L]
Related
I'm trying to rewrite /user/username to /user-profile/?user=username without changing the URL in the address bar. I have the following code:
RewriteRule ^/?user/(.*?)/?$ /user-profile/?user=$1 [L]
RewriteCond %{THE_REQUEST} ^/user-profile/\?user=([^\&\ ]+)
RewriteRule ^/?user-profile/$ /user/%1? [L,R=301]
This changes /user/username to /user-profile/?user= in the address bar. The query string var "user" is left blank but it somehow loads the correct user profile. So I think the first rule is working but the second rule and it's condition must not be since the URL in the address bar is changing. What can I do to fix it?
Thanks!
A couple of things. You have 2 rules, one that internally rewrites and the other redirects the browser. You must have your redirect come before the rewrite, otherwise the internal rewrite gets reprocessed by the redirect rule. The other thing is the %{THE_REQUEST} variable is literally the first line of the HTTP request, and it starts with a method, not the request URI:
GET /user-profile/?user=qwerty HTTP/1.1
is what it will look something like. That means you need to alter your regex to account for those other parts of the request that's not the URI. Try:
RewriteCond %{THE_REQUEST} ^GET\ /user-profile/\?user=([^\&\ ]+) [NC]
RewriteRule ^ /user/%1? [L,R=301]
RewriteRule ^/?user/(.*?)/?$ /user-profile/?user=$1 [L]
I have problem when I try to redirect and rewrite together.
I have site example.com/show_table.php?table=12 (max 99 tables). I wanted nice links, so I got this .htacces rw rule:
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
Now are links something like example.com/table/12 - it's definitely OK. But I want all old links redirect to new format. So I use Redirect 301, I added to .htaccess this code:
RewriteCond %{REQUEST_URI} show_table.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
But when I visit example.com/show_table.php?table=12, I receive just redir-loop. I don't understant - the first is rewrite, the second is redirection, there ain't no two redirections. Do You see any error?
Thanks!
Instead of checking REQUEST_URI in the condition, you need to be checking in THE_REQUEST (which contains the full original HTTP request, like GET /show_table.php HTTP/1.1). When Apache performs the rewrite, it changes REQUEST_URI, so to the rewritten value, and that sends you into a loop.
# Match show_table.php in the input request
RewriteCond %{THE_REQUEST} /show_table\.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
# Do a full redirection to the new URL
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
# Then apply the internal rewrite as you already have working
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
You could get more specific in the %{THE_REQUEST} condition, but it should be sufficient and not harmful to use show_table\.php as the expression.
You'll want to read over the notes on THE_REQUEST over at Apache's RewriteCond documentation.
Note: Technically, you can capture the query string in the same RewriteCond and reduce it to just one condition. This is a little shorter:
# THE_REQUEST will include the query string so you can get it here.
RewriteCond %{THE_REQUEST} /show_table\.php\?table=([0-9]{1,2})
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
I'm trying to 301 redirect a paginated blog list from an old site onto a new url.
I think I'm getting pretty close with the RewriteRule but I'm not quite there yet, this is what I have:
RewriteCond %{QUERY_STRING} ^page=
RewriteRule ^(blog)?$ http://www.newdomain.com/news/page/$1? [R=301,L]
Using this rule if I go to
http://www.olddomain.com/blog?page=1
I currently get redirected to
http://www.newdomain.com/news/page/blog
I would like to be sent to
http://www.newdomain.com/news/page/1
I'm sure its just something small and simple that I'm missing.
Edit
Expanding on the solution below, I've added tags/category support to the rewrite rule using $1.
RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
RewriteRule ^blog/tag/([^/\.]+)?$ http://www.newdomain.com/news/tag/$1/page/%1? [R=301,L,NC]
Few minor mistakes in your code.
You need to capture page parameter's value from query string first
Then use that capture value using % instead of $1
No need to capture blog since you don't need it.
Change your code with:
RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
RewriteRule ^blog/?$ http://www.newdomain.com/news/page/%1? [R=301,L,NC]
So I have a kind of strange trouble. I have a rewrite rule from
mysite.com/brand.php?brand=example&ref=ex1
to
mysite.com/brand/example/ex1
And I managed to do that, but there is also another problem: when you type this:
mysite.com/brand/example/ex1.html
or
mysite.com/brand/example/ex1.php
I get this:
mysite.com/brand/example/ex1?ref=ex1.php&brand=example
Can anyone tell why I get this redirect and how to get rid of it?
I tried this:
RedirectMatch ^brand/([A-Za-z0-9-\.\_]+)/([A-Za-z0-9-\.\_]+)php$ /brand/$1/$2
or this:
RedirectMatch ^brand/([A-Za-z0-9-\.\_]+)/([A-Za-z0-9-\.\_]+)\.php$ /brand/$1/$2
but it simply doesn't work.
Please, anyone help me!
I would use the mod_rewrite for this:
RewriteEngine On
RewriteCond %{REQUEST_URI} =/brand.php
RewriteCond %{QUERY_STRING} ^brand=([^&]+&ref=(.+)$
RewriteRule .* /brand/%1/%2? [L,R]
The RewriteRule is applied only when both RewriteCond are valid. The first RewriteCond compares the request URL to be /brand.php, the second matches regular expression against the query string and stores the matches in %1 and %2 respectively.
In the RewriteRule is the URL rewritten using the matched parts from query string.
Need help with an 301 htaccess redirect rule doing the following:
www.name.com/wordA/wordB/* to www.name.com/WordNEW/wordA/wordB/*
we are basically adding "WordNew".
I have three wordA and five wordB for a total of 15 path variations.
Spin this on for size:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/WordNEW [NC]
RewriteRule ^(.*)$ /WordNEW/$1 [L,R=301]
Broken down
RewriteCond %{REQUEST_URI} !^/WordNEW [NC]
Check the requested URI does not start (!^) with the same folder path (/WordNEW) as the final. If it does, you've already redirected once or you are already there. If you don't check then you can end up on a loop rewriting the path over and over again.
RewriteRule ^(.*)$ /WordNEW/$1 [L,R=301]
If it passes, grab the whole requested URI (^(.*)$) and prepend with the new folder path (/WordNEW/$1). Then flag this as the last rule (L) in the RewriteRule while redirecting under 301 (R=301).
if WordNEW is in a constant position my quick and dirty solution would be to split the url string on '/'. at index 1 I would prepend the string WordNEW/ . For a more feasible solution I will need a few moments to think.
Here’s a simpler rule:
RewriteRule !^WordNEW/ /WordNEW%{REQUEST_URI} [L,R=301]