How can I use htaccess redirect rules? - apache

I want to redirect request to sample.php if pattern was like below
sample.com/vacation
I tried this code but not work correctly, redirect all request contain vacation such as sample.com/anything/vacation
RewriteCond %{REQUEST_URI} /vacation
RewriteRule ^ /sample.php [L,R]
how can I achieve that ?

With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^vacation/?$ sample.php [NC,L]

Related

Htaccess rewrite with parameter value

I'm looking for some help. I wanted to redirect the following paramters to another url, how can I make this possible? I have googled alot of other solutions but I fail to make this one work.
https://(www).website.com/reservation#!/confirm?code=a1XapXsCmlaC0
to
https://new-website.com/test/page#!/confirm?code=a1XapXsCmlaC0
Attempted code:
RewriteRule \/reservation#!\/confirm\?code=$ https://test.com/$1
"grep"-ing the code value would make it much easier.
Any help is much appreciated.
Thanks in advance
You may use this redirect rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?website\.com$ [NC]
RewriteRule ^reservation/?$ https://test.com/test/page [L,NC,R=301]
Query string and fragment i.e. part after # will automatically be forwarded to the new URL.
Based on your shown samples, could you please try following. Where you want to redirect from (www).website.com to new-website.com in url.
Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*) [NC]
RewriteRule ^ https://new-%1/test/page#!%{REQUEST_URI} [QSA,NE,R=301,L]

.htaccess redirect to subdomain url with hash

I'm trying to redirect a site with the following url structure:
https://example.com/2021/about
to
https://2021.example.com/#about
https://example.com/2021/visit
to
https://2021.example.com/#visit
how can i do this?
i tried adding this to the /about directory in the original domain:
RewriteEngine on
RewriteBase /
RewriteRule (.*) https://2021.example.com/#about [R=301,NE, L]
but what i got after the redirect was
https://2021.example.com/#about2021/about
which is not right. any help is appreciated
[EDIT] i only want to apply this to some folders, like /2021/about and 2021/visit
You may use this redirect rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com) [NC]
RewriteRule ^(20\d{2})/(.+?)/?$ https://$1.%1/#$2 [R=301,L,NE]
Make sure this is your topmost rule in .htaccess and you clear your browser cache before testing this new rule.
Could you please try following, written based on your shown samples. Please make sure to clear your browser cache before testing your URLs. Following will only be applied to 2021/about OR 2021/visit uris.
RewriteEngine ON
RewriteCond %{HTTP_HOST} (example\.com) [NC]
RewriteRule ^(2021)/(about|visit)/?$ http://$1.%1/#$2 [R=301,NC,NE,L]

apache rewriterule causing redirect loop

Im trying to make an apache(2) RewriteRule which is using a QUERY_STRING to redirect a user to a friendly to read URL, but it is causing a redirect loop.
What I want to achieve is that when a user requests for the index.php?layout=751&artnr=# URL, that this request gets redirected to /another/page/#.
And in a way the Redirect works, but Apache keeps redirecting the user once it has reached the /another/page page
The RewriteRule looks like this:
RewriteCond %{REQUEST_URI} ^/index.php
RewriteCond %{QUERY_STRING} ^layout=751&artnr=(.+)$
RewriteRule ^index\.php$ another/page/%1? [R=301,L]
I've been searching alot of issues about this situation but none of them really have the answer that solves my problem, since those problems don't use a QUERY_STRING.
I have also tried to add the RewriteOptions maxredirects=1 option, but this doesn't solve the problem either.
Any help would be appreciated.
Thanks in advance.
Use THE_REQUEST variable instead of REQUEST_URI like this:
RewriteCond %{THE_REQUEST} /index\.php\?layout=751&artnr=([^\s&]*) [NC]
RewriteRule ^ another/page/%1? [R=301,L,NE]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.
This assumes a RewriteBase has been set above this rule.
Clear your browser cache or use a new browser to test this change.

Htaccess redirections in apache

I'm trying to perform the following permanent redirections in .htaccess file, but I can not make them work. Can you help me?
Thank you very much. Regards!
Redirects are these:
http://www.mapfre.com.pa/productos/#39 to https://www.mapfre.com.pa/seguros-pa/seguros/seguros-salud/
http://www.mapfre.com.pa/productos#36 to https://www.mapfre.com.pa/seguros-pa/seguros/seguros-auto/
http://www.mapfre.com.pa/?post_type=corporativo&p=21 to https://www.mapfre.com.pa/seguros-pa/sobre-mapfre-panama/historia/
There's no way to make server-side redirect for URLs with an anchor #.
The last one may be done by this rule:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^post_type=corporativo&p=21$
RewriteRule ^$ /seguros-pa/sobre-mapfre-panama/historia/ [R=301,L]

Mod Rewrite, Unexpected Results

We are trying to redirect everything from one domain to another with the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example2.com%{REQUEST_URI} [R=301,L]
When we visit http://www.example.com/v2sc
We are being redirected to http://www.example2.comv2sc
We would like to be redirected to http://www.example2.com/v2sc considering www.example2.comv2sc is not a valid hostname
Any ideas on how we can accomplish this?
Thank you!
It seems like you're using a .htaccess file for this. In that context the leading slash is not present in %{REQUEST_URI} so it's up to you to put it back in.
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example2.com/%{REQUEST_URI} [R=301]
Please also note that solutions like this should be used only if you cannot edit the main server configuration file. Doing so would allow you to use a cleaner combination of vhosts and Redirect directives that would run much more quickly.