.htaccess rewrite rule causes endless loop - apache

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)

Related

Redirect to same domain with htaccess

I want to redirect a user via htaccess when a specific HTTP_REFERRER is set. Here is my current htaccess
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^(.*)\.example.org [NC]
RewriteRule ^(.*)$ https://example.com/blocked/$1 [L,R]
The problem I am facing now is that the HTTP_REFERRER still persists and I end within an endles redirection to /blocked/blocked/blocked
Is there any easy way to simple redirect/forward the user just ones to /blocked?
You need to exclude the /blocked URI in Rewrite to avoid the loop error
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^(.*)\.example.org [NC]
RewriteCond %{REQUEST_URI} !/blocked
RewriteRule ^(.*)$ https://example.com/blocked/$1 [L,R]

Redirect from main page

I have a conditions:
a) Redirect from help.example.com to example.com/support
b) Redirect from other page, like help.example.com/catalog to example.com/catalog
This all I do in .htaccess file.
My code redirect me only on example.com/support
RewriteCond %{HTTP_HOST}${REQUEST_URI} ^help\.example\.com/(.+)
RewriteRule ^(.+)$ example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST}${REQUEST_URI} ^help\.example\.com
RewriteRule ^(.*)$ example.com/support/ [R=301,L]
How can I resolve this problem?
Please try this rules for yours a - b conditions:
a) Redirect from help.example.com to example.com/support
RewriteCond %{HTTP_HOST} ^help\.example\.com$ [NC]
RewriteRule ^(/?)$ https://example.com/support [R=301,L]
b) Redirect from other page, like help.example.com/catalog to example.com/catalog
RewriteCond %{HTTP_HOST} ^help\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
With your shown samples/attempts, please try following htacces rules in your htaccess file. Please place these rules at top of your file, also make sure to clear your browser cache before testing your URLs.
This will catch help.example.com OR www.help.example.com both kind of urls.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?help\.example\.com$ [NC]
RewriteRule ^/?$ https://example.com/support [NE,R=301,L]

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

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]

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]

Apache redirect subdomain to folder, keep parameters

I have this code in .htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
# If empty subdomain, replace with "www"
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]
# If subdomain isn't empty and not "www", redirect to "folder"
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301]
#PAGES REDIRECTION
RewriteRule ^(.*)/register/ /index.php?sub=$1&page=register
RewriteRule ^(.*)/register /index.php?sub=$1&page=register
RewriteRule ^(.*)/lostpass/ /index.php?sub=$1&page=lostpass
RewriteRule ^(.*)/lostpass /index.php?sub=$1&page=lostpass
...
(a rule for wildcard subdmains is already in place and working)
If I browse to http://test.example.com it redirects correctly to http://www.example.com/test but when I try to browse to http://test.example.com/register, it actually redirect to http://www.example.com/test/index.php?sub=http://www.example.com/test&page=register which should redirect to http://www.example.com/test/register
What am I doing wrong here? Thanks in advance!
Try adding the L flag to your second redirect rule, similar to how have it in the first.
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301,L]
It looks like your rewritten URI is passing through to the next rule.
Also, I don't think your first two RewriteCond are in the correct spot.