How can I set the RewriteCond for the exact URL only? - apache

I need to make a hidden redirect from sitename.dom to sitename.dom2 keeping the rest of string untouched.
For now I use:
RewriteCond %{HTTP_HOST} ^sitename.dom
RewriteRule ^(.*) http://sitename.dom2/$1 [P]
and it works perfectly. But. Due to multilanguage on my website the frontpage has the following path:
sitename.dom2/lang
thats why when user calls sitename.dom he is being redirected (hidden) to sitename.dom2/ and he's getting 404 page.
So, please advise how do I make a strict redirect for exact request only sitename.dom without any further?
I had tried
RewriteCond %{HTTP_HOST} ^sitename\.dom$
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*) http://sitename.dom2\/lang [P]
with no luck.
BTW, inside the website language subpath doesn't affect at all. sitename.dom/lang/page works as good as sitename.dom/page

I had to add the single redirect before all other. And use dom2 instead of dom1 in this rule.
Here is the solution:
RewriteEngine On
#redirect front page only:
RewriteCond %{HTTP_HOST} ^sitename\.dom2$ [OR]
RewriteCond %{HTTP_HOST} ^www\.sitename\.dom2$
RewriteRule ^/?$ "http\:\/\/sitename\.dom2/\lang" [L]
#redirect all other pages:
RewriteCond %{HTTP_HOST} ^sitename\.dom2
RewriteRule ^(.*) http://sitename\.dom1\/$1 [P]

Simply add /lang in your first RewriteRule directive:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sitename.dom$
RewriteRule ^ http://sitename.dom2/lang%{REQUEST_URI} [P]

Related

Need to redirect 301 a URL in .htaccess file but it adds extra http//?

I am trying to redirect /abc.html to /abc.php but when I did it gives an extra http// and page is not working like http//www.example.de/abc.php don't know from where this HTTP comes.
note: website is not with ssl so domain name is http://example.de
My .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.de/$1 [R=301,L]
RedirectPermanent /tour.html /tour.php
With your shown samples/attempts, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
##To serve home page link.
RewriteRule ^/?$ index.php [L]
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^([^.]*)\.html/?$ $1.php [NC,L]

.htaccess Redirect all pages except one and another internal redirect of a certain domain

I want to redirect all request of one particular domain to another, except for one page request. I hope my attempt explains what I try to do:
RewriteEngine on
Rewritecond %{HTTP_HOST} !^host\.mysite\.com
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L]
Rewritecond %{HTTP_HOST} !^host\.mysite\.com
RewriteCond %{REQUEST_URI} !^/link/
RewriteCond %{REQUEST_URI} !^dokument_by_link\.php
RewriteRule (.*) https://www.anothersite.de/$1 [R=302,L]
#This should not apply to host.mysite.com, but I think this is already accomplished by the L-flag above
RewriteRule ^test/?$ test.php [L,NC]
host.mysite.com/link/ should be redirected to host.mysite.com/document_by_link.php
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L]
All other requests should be redirected to https://www.anothersite.de
Thank you very much!
This should do the trick :
RewriteEngine on
#rewrite /link to /document_by_link.php
RewriteRule ^link/?$ /document_by_link.php [L]
#redirect all other URLs to https://www.anothersite.de
RewriteRule !dokument_by_link\.php$ https://www.anothersite.de%{REQUEST_URI} [L,R]
This should work for you:
RewriteEngine On
Rewritecond %{HTTP_HOST} ^host\.mysite\.com$ [NC]
RewriteCond %{THE_REQUEST} !\s/link/ [NC]
RewriteRule ^ https://www.anothersite.de%{REQUEST_URI} [R=302,L,NE]
Rewritecond %{HTTP_HOST} ^host\.mysite\.com$ [NC]
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L,QSA]
RewriteRule ^test/?$ test.php [L,NC]
Using THE_REQUEST instead of REQUEST_URI here as REQUEST_URI may change to a rewritten URI whereas THE_REQUEST remains same for the scope of a web request.
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Keep path when rewriting with .htaccess

I recently moved my webpage from HTTP to HTTPS and I now got a couple of things that doesn't work like before.
The problem I'm having is that when a user is trying to go to:
http://example.com/i/<imageID> they are redirected to:https://www.example.com/i/image.php?id=<imageID>. This is "correct", but not what I want. I want the links to stay at /i/<imageID> regardless of the user came from http or https.
I have two .htaccess-files that I use to control this redirection on my webpage.
First .htaccess (root directory):
RewriteEngine on
RewriteOptions inherit
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Second .htaccess (image directory - /i):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^=]+)$ image.php?img=$1 [NC,L]
This redirection was working just fine before I added the first .htaccess-file that redirects the user from HTTP to HTTPS, but now it seems to be broken.
I'm not that familiar with htaccess-files so I hope someone out there can help so I can get my clean URLs back. :)
UPDATED ANSWER --> updated again
%{REQUEST_URI} only includes the URI WITHOUT the querystring. But this is stored in and can be retrieved in another variable --> %{QUERY_STRING}
I edited the code accordingly for the first .htaccess-file
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}%{QUERY_STRING} [L,R=301,QSA]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}%{QUERY_STRING} [L,R=301,QSA]
If you change your second .htaccess file to the following, it should be clean :)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/i/image.php
RewriteRule ^/i/(.*) /i/image.php?img=$1 [P,NC,L,QSA]
The [P] stands for 'proxy' meaning that your Apache is seen by the client as a proxy (ofc, without his notice), because your Apache executes the actual (correct) request, while the client only sees the result.
My new take on your problem
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteRule ^(.*)$ https://%2$1 [R=Permanent,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^=]+)$ image.php?img=$1 [NC,L]

Redirection is not working with mod_rewrite in htaccess

I need to redirect few URIs having query string like:
/pages/foo.bar?pageId=123456 to http://some.site/spam/egg/
/pages/foo.bar?pageId=45678 to http://another.site/spaming/egging/
I have this in my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=123456$
RewriteRule ^.*$ http://some.site/spam/egg/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=45678$
RewriteRule ^.*$ http://another.site/spaming/egging/ [R=301,L]
But its not working, showing 404. What am i doing wrong?
You need to move these 2 rules i.e. before all other rules just below RewriteEngine On line as other rules might be overriding this.
(Based on your comments) Your culprit rule is this rule:
RewriteRule . index.php [L]
Which is actually rewriting every request to index.php and changing value of REQUEST_URI variable to /index.php thus causing this condition to fail:
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
From your example, you get redirected to
http://some.site/spam/egg/?pageId=123456
http://another.site/spaming/egging/?pageId=45678
You can use your browser developer tools to see the redirection (in the Network tab).
Maybe the query strings in the redirected URL lead to a 404? You can add a ? at the end of your redirection to clear the query string:
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=45678$
RewriteRule ^.*$ http://another.site/spaming/egging/? [R=301,L]

.htaccess rewrite rule causes endless loop

I want my .htaccess file to redirect to some page if any wildcard as a subdomain entry hit the browser. i.e. I want
sam.xyz.com
To redirect to
sam.xyz.com/view.php?id=sam
I am using following rewrite rules for redirect.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com
RewriteRule ^(.*)$ /view.php?id=%1 [L,R]
Problem i am facing is that it does not shift to new domain keeping query string instead it generates an endless loop
sam.xyz.com
redirects to
http://sam.xyz.com/view.php?id=sam
But doesnt move to url above without endless loop.
Kindly help me out.
Thanks in advance,
you should add a condition for redirect to prevent redirection loop:
RewriteCond %{REQUEST_URI} !^/view\.php
the whole code would be:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com
RewriteCond %{REQUEST_URI} !^/view\.php
RewriteRule ^(.*)$ /view.php?id=%1 [L,R]
You are redirecting to: prefix.domain.tld/view.php?id=prefix
Ensure that the url does not contain: id=prefix.
This solution prevents, that someone call's the url: aaa.example.com/view.php?id=bbb
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com
RewriteCond %1::%{QUERY_STRING} !^([^:]+)::.*id=\1
RewriteRule ^ /view.php?id=%1 [L,R]
Note: (.*) in the rewrite rule is obsolete.
Leave the R away to do not redirect the visitor to the url (/view.php?id=%1)