Redirect 301 query string htaccess - .htaccess

I've been trying to redirect query string link to another link in my .htaccess file I've been following some guides and I figured out how to do that so at the moment the redirect is done but it doesn't really work like I would like.
This is the first link:
http://www.mydomain.it/index.php?page=lkr_pg_chisiamo
That should be redirected to this one:
https://www.mydomain.eu/chi-siamo/
This is what I've done in my htaccess in wordpress:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{QUERY_STRING} ^page=lkr_pg_chisiamo$
RewriteRule ^(.*)$ https://www.mydomain.eu/chi-siamo/ [R=301,L]
Now the redirect works but my final URL is:
https://www.mydomain.eu/chi-siamo/?page=lkr_pg_chisiamo
What could the problem be?

Now the redirect works but my final URL is:
https://www.mydomain.eu/chi-siamo/?page=lkr_pg_chisiamo
You need to discard the query string. Two solutions:
Solution 1 (using ? to reset new query string)
RewriteRule ^(.*)$ https://www.mydomain.eu/chi-siamo/? [R=301,L]
Solution 2 (using QSD flag)
RewriteRule ^(.*)$ https://www.mydomain.eu/chi-siamo/ [R=301,L,QSD]

Related

Redirect urls with question mark and other parameters in htaccess

I want to redirect pages like:
/category-name/post-name.html?id=1234
To:
/category-name/1234-post-name.html
How can do this using htaccess?
What I have tried:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^([^/]+)/([^.]+)\.html$ /$1/%1-$2\.html [L,R=301]
But it is a continuous redirect.
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([\w-]+)/([\w-]+)\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%3-%2? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^([\w-]+)/([^-]+)-([\w-]+)/?$ $1/$3?id=$2 [L,QSA]
Try this
Make sure the url is root url
Example:-
www.foo.com/category-name/post-name.html
It will only work if the project url is same as that of the above.
www.foo.com/blog/category-name/post-name.html
This won't work you need to update the RewriteBase url accordingly l.
This is the conditions
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule here
RewriteRule ^category-name/([0-9]+)-post-name$ /category-name/post-name.html?id=$1 [L]
This should work..

Htaccess redirect with query

I'm having an issue with a query string, although I'm not sure what i'm tryign to do is possible.
I have the following url with a search query attached:
subdomain.mysite.com/search/?search=searchquery
but I need it to redirect to the following url including the query string:
subdomain.mysite.com/search/?rs=searchquery
I was wondering if this is possible with a mod_rewrite?
I've tried the following:
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([0-9]*)$
RewriteRule ^(.*)$ http://subdomain.mysite.com/search/?rs=% [L,R=301]
I don't know if my Syntax is incorrect or i'm just barking up the wrong tree. Any help would be awesome.
This is the Start of my HTAccess File - This is a Wordpress site with the rewrite rule provided by user: Amine Hajyoussef:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([^&]*)$
RewriteRule ^(.*)$ /search/?rs=%1 [L,R=301]
</IfModule>
Unfortunately this doesnt appear to work? Any ideas?
you forgot the 1 after %, i have also changed the search string so it can be anything instead of just number.
RewriteCond %{REQUEST_URI} ^/search/?$
RewriteCond %{QUERY_STRING} ^search=([^&]*)$
RewriteRule ^(.*)$ /search/?rs=%1 [L,R=301]

Redirect only hompage and not query string urls

I am currently using the following to redirect just the homepage of my site
RedirectMatch 301 ^/$ http://www.example.com/it/
However, it is also redirecting urls which have the following format
so the following url
http://www.example.com/?act=25
becomes
http://www.example.com/it/?act=25
Is there a way i can tell the htaccess rule not to redirect urls with that format i.e. starting with a query string?
used Mod-rewrite
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{QUERY_STRING} !^act=(.*)$ [NC]
RewriteRule ^$ http://www.example.com/it/

Redirect a subdomain to a dynamically generated folder and mask theurl

I currently have a blog setup as the subfolder of the main site, mydomain.com.au/blog/, however the /blog is dynamically generated through PHP and doesn't physically exist.
I have created a subdomain blog.mydomain.com.au and I am trying to get it to display everything just like mydomain.com.au/blog/ but mask the url so that it shows blog.mydomain.com.au.
I have made several attempts using htaccess and got close a couple of times, but there's always something wrong. Below are the 2 attempts which got me the closest to the expected results:
1st attempt
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com\.au$
RewriteRule ^.*$ http://www.mydomain.com.au/blog%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This will get blog.mydomain.com.au to redirect to www.mydomain.com.au/blog/, but does not mask the URL (the address bar will show www.mydomain.com.au/blog/).
2nd attempt
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com\.au$ [NC]
RewriteRule blog/(.*) http://blog.mydomain.com.au/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This got me the closest. It redirects and masks the URL just the way I want it to for all pages under /blog/ (e.g. blog.mydomain.com.au/whatever/ redirects to www.mydomain.com.au/blog/whatever/ but masks the URL as blog.mydomain.com.au/whatever/), but it doesn't do it for blog.mydomain.com.au, which gets redirected to www.mydomain.com.au.
You have two different requirements here. The first one is a rewrite, which will work, if the request is blog.mydomain.com.au and you remove the domain part and the R flag in the rule substitution
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com\.au$ [NC]
RewriteRule !^blog/ /blog%{REQUEST_URI} [L]
The second one is an actual redirect, if the request is www.mydomain.com.au/blog/
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com\.au$ [NC]
RewriteRule blog/(.*) http://blog.mydomain.com.au/$1 [R,L]
When everything works as you expect, you can change R to R=301.
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

htaccess rewrite %3F and %3D to ? and =

I'm wanna rewrite all incoming URLs who at end have %3Ffull%3D1 to ?full=1
I'm try this in htaccess but not work:
RewriteRule ^(.*)%3F(.+)%3D(.+)$ $1?$2=$3 [R=301]
here is full .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)%3F(.+)%3D(.+)$ $1?$2=$3 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I recently implemented the following .htaccess code to accomplish this - I've modified it to fit your scenario of using the "full" parameter.
# replace %3F with ? and %3D with =
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\%]+)\%3[Ff]full\%3[dD]([^\ ]+)\ HTTP/
RewriteRule \.*$ http://www.mywebsite.com/%1?full=%2 [R=301,L]
Try adding the L flag in the rule that handles the query string:
RewriteRule ^(.*)%3F(.+)%3D(.+)$ $1?$2=$3 [L,R=301]
otherwise, the rewritten URI could end up getting handled by the last rule (RewriteRule . /index.php [L]) and yet still get redirected because the URI was flagged for a 301. Also, if the original URL that you are trying to redirect contains %3Ffull%3D1 in the query string itself, then your rule isn't going to match that, you'd need something like:
RewriteCond %{QUERY_STRING} ^(.*)%3Ffull%3D1(.*)$
RewriteRule ^(.*)$ $1?%1&full=%2 [L,R=301]

Resources