apache RewriteCond doesn't match empty http_referer - apache

I want to realize something like this:
When someone opens a browser and access http://www.example/controller.php directly, then the request will be redirected to the error page. However, if a user clicks a link with http://www.example/controller.php on my website, the request will be redirected to http://www.example/controller
To do so, I write the following rewriterule in .htaccess file:
RewriteCond %{REQUEST_URI} ^/controller\.php
RewriteCond %[HTTP_REFERER] ^$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/controller/error [R,L]
But unfortunately RewriteCond %[HTTP_REFERER] ^$ is not matched. My all other rewriterules are working fine. To discard the side effect of other rewriterules, I delete all the other rules but it still doesn't work.
So I guess maybe %[HTTP_REFERER] is not empty when a url is directly accessed from the browser. Then I write the following rule right before the rewriterule above:
RewriteRule (.*) https://www.google.com/?referer=%{HTTP_REFERER} [R=301,L,QSA]
And this redirects to https://www.google.com/?referer=
So %[HTTP_REFERER] is well and truly empty.
I completely have no idea now, and any advice is greatly appreciated.

Related

redirect issue with rewrite rule $tag

Having some problems with this below; Specifically with the first rule.
^pages/articles/marketArticles\.php?id=31 I think here is the problem
, So basically what I'm trying to achieve is that when a user goes to /pages/articles/marketArticles.php?id=31 the user is redirected to /01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022/ this rule has to work with multiple different ids.
RewriteRule ^pages/articles/marketArticles\.php?id=31 /01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022 [R=301,L]
RewriteRule ^01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022/?$ /pages/articles/marketArticles.php?id=31 [END]
With the above example when the user goes to /01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022/ the user can view the page. When the user goes to pages/articles/marketArticles.php?id=31 the user is presented the page but is not redirected to the new url.
With your shown samples/attempts, please try following htaccess rules file. Make sure that your htaccess file is residing along side with 01-02-2022 folder(not inside it, along side it).
Please make sure to clear browser cache before testing your URLs.
RewriteEngine ON
##Rules when user hits /pages/articles/marketArticles\.php?id=31 to redirect TO 01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022/
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/pages/articles/marketArticles\.php\?id=31\s [NC]
RewriteRule ^ /01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022? [R=301,L]
##Rules when user hits 01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022 it rewrites to pages/articles/marketArticles.php?id=31 in backend.
RewriteRule ^01-02-2022/Top-5-Digital-Assets-to-watch-out-for-in-2022/?$ /pages/articles/marketArticles.php?id=31 [NC,QSA,L]

What is the correct syntax for "if host is not foo, redirect to bar" in a .htaccess file?

This website has a ton of extra domains (note: these are not subdomains; one of them, for instance, is http://eduard.fi) that the owner (or the SEO people, rather) wants to redirect to the main domain. Instead of listing them one by one, this is what I tried:
RewriteCond %{HTTPS_HOST} !^masetti\.fi$
RewriteRule ^(.*)$ https://masetti.fi/$1 [R=301,L]
However this creates a redirect loop. Why is that? This does not produce a server error, so for that part the syntax is correct, but it does not do what I want.
You were close, but made a logical mistake. Take a look at this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^masetti\.fi$
RewriteRule ^(.*)$ https://masetti.fi/$1 [R=301]
An alternative would be that:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^masetti\.fi$
RewriteRule ^ https://masetti.fi%{REQUEST_URI} [R=301]
The RewriteCond has been slightly altered: It is the variable %{HTTP_HOST} you want to check, not %{HTTPS_HOST}which does not exist.
PS: it is a good idea to start out with a 302 redirection and only change that to a 301 once everything works as intended. That prevents issues with client side caching.

.htaccess redirect to another domain except admin page

I am asking because I can not for the live of me figure out what is wrong and so far none of the StackOverflow answers worked.
I have to redirect a domain to another subdomain, except the admin. For example:
sub1.domain.com/testsite/ shoud redirect to "sub2.domain.com/testsite/",
but sub1.domain.com/admin/ or "sub1.domain.com/de/admin/" should stay right where it is.
As a first step I tried to only check for the "admin", so everything would be redirected except "sub1.domain.com/admin/":
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com
RewriteCond %{REQUEST_URI} !^\/admin
RewriteRule ^/?(.*)$ http\:\/\/sub2\.domain\.com%{REQUEST_URI} [R=301,L]
This one looked most promising, but it is not working. The second condition is not working and the admin page still gets redirected.
If anyone can help I would appreciate it.
EDIT:
I should have said that its a multi-domain site, which means we have a .htaccess file for all sites and that is the reason I specifically check for the domain.
I'm just posting this, but I can't test it!
But I guess this redirects EVERYTHING except that one domain.
RewriteCond %{HTTP_HOST} !^sub1.domain.com/admin/ [NC]
RewriteRule ^/(.*)$http\:\/\/sub2\.domain\.com%{REQUEST_URI} [R=301,L,NC]
I hope it works!
So,
I just found the reason (besides my stupidity). The site I should redirect was a Drupal Site. Thats why all links end up at the same location:
sub1.domain.com/index.php
The reason why my above Rewrite Condition was not working is, that sub1.domain.com/admin is being redirected to sub1.domain.com/index.php, which consequently ends up at: "sub2.domain.com/index.php". The correct rewrite rule looks like that:
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com
RewriteCond %{REQUEST_URI} !^/(admin|index\.php|de\/admin|it\/admin|user|de\/user|it\/user)
RewriteRule (.*) http://sub2.domain.com%{REQUEST_URI} [R=301,L]
This redirects everything except:
sub1.domain.com/admin
sub1.domain.com/de/admin
sub1.domain.com/it/admin
sub1.domain.com/user
sub1.domain.com/de/user
sub1.domain.com/it/user
and of course
sub1.domain.com/index.php
Since the last one also should not be redirected if the user types it in directly, it is not a perfect solution, but I can live with it.
RewriteCond is use to check condition weather to execute .htacess or not
For your case the solution may be as below:-
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(admin)$ http://sub1.domain.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://sub2.domain.com/$1 [R=301,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 redirect to a specific url on same domain (without looping)

I really hope you can help me out (it is driving me crazy).
I've tried dozens of setups and nothing seems to work, Googled myself dizzy and tried numerous different setups, but it all seems to result in a loop or a server error.
This is what needs to happen:
I have a site with multiple domains attached to it. What I need is that when someone visits the website via the "domain.co.uk"-domain, a redirect to the correct language parameters (among others) takes place.
To be very specific: when visiting via "www.domain.co.uk" the visitor must be redirected to "www.domain.co.uk?lang=en&noredir=1&currency=3"
I've made sure that the www is present with this:
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
The trouble is (I think) the redirect within the same domain without causing a loop.
I've tried stuff like this, but with no result:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]
Hope you can help,
Cheers!
This will cause a loop:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]
Because you're only checking the host header. Every time the redirect fires it will arrive back at the server with a host header of www.domain.co.uk and redirect again. You need to also check the query string and only redirect if it doesn't already match what you sent:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewrteCond %{QUERY_STRING} !lang=en&noredir=1&currency=3
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]