.htaccess 301 redirect Apache - php

I am really having trouble with this Rewrite Rule
RewriteRule ^index.php?a=profile&u=(.*)$ /profile/$1 [R=301]
I want to make 301 redirect like
mysite.com/profile/USERNAME

Querystring is not part of match in RewriteRule, you need to match against %{THE_REQUEST} var in RewriteCond and then use %n to get that match :
This should be your complete htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} /index\.php\?a=profile&u=([^\s]+) [NC]
RewriteRule ^ /profile/%1? [L,R]
RewriteRule ^profile/([^/]+)/?$ /index.php?a=profile&u=$1 [NC,L]

Try
RewriteCond %{QUERY_STRING} ^a=profile&u=(.*)$
RewriteRule ^index.php /profile/%1 [L,R]

Related

I want to redirect specific url to non https using htaccess

my current htaccess is:-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
i want to add rule to htaccess for url like https://www.example.com/abc/arg1/arg2 should redirect to http://www.example.com/abc/arg1/arg2
for this https://www.example.com/abc/* it should redirect to non-https format with keeping all arguments.
Use below rule, check after clearing your cache.
RewriteCond %{REQUEST_URI} ^/abc/(.*) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Edit: Please try with below rule,
RewriteCond %{REQUEST_URI} !^/abc/(.*) [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In above rule we are appending https to all url which are not starting with abc/(.*) you have to use this rewritecond where your original https rule exists.
If you want to redirect all https requests starting with abc/ to http, you must check for both abc/ and HTTPS
RewriteCond %{HTTPS} on
RewriteRule ^abc/ http://www.example.com%{REQUEST_URI} [R,L]
When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.
I am not sure how you achieve the job, but the following should be working perfectly for you.
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/abc [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

url should rewrite if page and query string is match

I want to redirect page if my url and query string is matched i have try much but no success, can you please told me where i done mistake.
My url is
http://xxx.xxx.xxx.xx/search/videos?search_query=xxx+xxx
and I want to redirect (rewrite using .htaccess) it to
http://xxx.xxx.xxx.xx/search/videos/xxx+xxx
I have tryed
RewriteCond %{QUERY_STRING} ^search_query=([A-Za-z0-9]*)$
RewriteRule ^search/videos$ search/videos/$1 [R=301,L]
RewriteCond %{QUERY_STRING} "search_query=" [NC]
RewriteRule ^(.*)$ search/videos/$1? [L,QSA]
RewriteCond %{QUERY_STRING} ^search_query=$1 [NC]
RewriteRule ^(.*)$ search/videos/$1? [L,QSA]
What i am doing wrong?
You can use the following rule
RewriteEngine on
#terminate the 2nd rewrite iteration
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# redirect "/search/videos?search_query?foobar" to "/search/videos/foobar"
RewriteCond %{QUERY_STRING} ^search_query=(.+)$
RewriteRule ^search/videos$ /search/videos/%1? [R=301,L]
#internally map "/search/videos/foobar" to "/search/videos?search_query?foobar"
RewriteRule ^search/videos/([^/]+)/?$ /search/videos/?search_query=$1 [NC,L]

Appending query string through redirect htaccess

I am trying to create a rewrite cond in htaccess
I am trying to redirect
http://www.example.com/business/search-results.php?keywords=Animal+Business+Cards&go=Search
to
http://www.example.com/business/search results.php?keywords=Animal+Business&go=Search
or if possible remove the word "+cards" from any search queries...
what I have tried so far
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Animal+Business+Cards$
RewriteRule ^(.*)$ http://www.example.com/business/search-results.php?keywords=Animal+Business [R=301,L]
also tried
RewriteCond %{QUERY_STRING} "&go=Search" [NC]
RewriteRule (.*) /$1? [R=301,L]
and this
Redirect /business/search-results.php?keywords=Animal+Business+cards&go=Search http://www.example.com/business/search-results.php?keywords=Animal+Business&go=Search
None of these working...
is it possible?
Can any one help?
Thank You in advance
Try the following code :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^keywords=([^\+]+)\+([^\+]+)\+([^&]+)&go=([^&]+)$ [NC]
RewriteRule ^ %{REQUEST_URI}?keywords=%1+%2&go=%4 [NC,L,R]
Another solution
RewriteEngine On
RewriteCond %{THE_REQUEST} \?keywords=([^\+]+)\+([^\+]+)\+([^&]+)&go=([^&\s]+) [NC]
RewriteRule ^ %{REQUEST_URI}?keywords=%1+%2&go=%4 [NC,L,R]
Note : Redirect directive does not support querystrings in its old path perameter, that is why your last attempt failed. You can use %{QUERY_STRING} or %{THE_REQUEST} variable to match against urls with query strings.

htaccess 301 redirect to rewriterule

I am changing my site URLs with .htaccess.
Until now, I was using
RewriteRule ^folder/([^/.]+)/p([0-9]+)/?$ folder/handler.php?name=$1&p=$2 [L]
Now, I want to change the "/p" for "-p".
I tried
RewriteRule ^folder/([^/.]+)/p([0-9]+)/?$ ^folder/([^/.]+)-p([0-9]+)/?$ [L, R=301]
But it gives me a 500 error when I try to access any of the two urls.
Is there any way to make a 301 from the first to the second one? Or any other choice?
Couple of issues:
You have space before R=301
On RHS (target side) of RewriteRule you have a regex
Keep your rules like this:
RewriteRule ^(folder/[^/.]+)/(p[0-9]+/?)$ /$1-$2 [L,NC,R=301]
RewriteRule ^folder/([^.-]+)-p([0-9]+)/?$ folder/handler.php?name=$1&p=$2 [L,NC,QSA]
This is how i do:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www*******com$ [NC]
RewriteRule ^(.*)$ www.******.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ www.*******.com/ [R=301,L]
Redirect /..../p /....-p

Redirect page with GET variable htaccess

Firstly,Thanks for reading this. How to rewrite the url , in htaccess
For example
wwww.domain.com/home?name=MuhdNazmi <---OLD URL
wwww.domain.com/home/MuhdNazmi <---NEW URL
name is a get variables,
I dont need put .php , since it code for not showing > .php
Try:
RewriteCond %{THE_REQUEST} \ /home\?name=([^&\ ]+)
RewriteRule ^ /home/%1? [L,R=301]
RewriteRule ^home/([^/]+)$ /home?name=$1 [L,QSA]
RewriteEngine On
RewriteRule ^home/([^/]*).php$ /home?name=$1 [L]
If it is a one on redirect then you can follow the below example for you redirect
RewriteEngine on
RewriteCond %{QUERY_STRING} name=MuhdNazmi$ [NC]
RewriteRule ^home/$ /home/MuhdNazmi? [L]
If you want to define it as a generic rule for all the query string parameter modify the above code as
RewriteEngine on
RewriteCond %{QUERY_STRING} name=(.*)$ [NC]
RewriteRule ^home/$ /home/%1? [L]

Categories