Apache redirect & rewrite - apache

This is my page
http://example.com/index.html .
I would like to make it work like this.
I would like to take the user to http://def.com when he requests
http://example.com/index.html
Even if he requests http://example.com/index.html?headers=0, he should be taken
to http://def.com
But if he requests the same URL with other query parameters, say for example http://example.com/index.html?footer=1, then the redirect or rewrite shouldn't happen. He should still be seeing the response from http://example.com/index.html?footer=1
I have already tried
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/index.html$ http://def.com

Change your htaccess file to this:
RewriteCond %{QUERY_STRING} ^(headers=0|)$ [NC]
RewriteRule ^index.html$ http://def.com [R=301,L]

Change your htaccess file to this:
RewriteCond %{QUERY_STRING} ^$ [OR]
RewriteCond %{QUERY_STRING} !headers
RewriteRule ^index.html$ http://def.com [R=301,L]

Related

Apache redirect using RewriteEngine with and without query string

I'm trying to get 2 redirections set on my htaccess file but I can't make it to work.
The desired output is:
if the user goes to www.mywebsite.com/page1, it gets redirected to /folder/newPage
if the user goes to www.mywebsite.com/page1?a=123, it gets redirected to /folder/anotherPage?a=123
I tried this
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/page1$ /folder/newPage [R=301,L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/page1(.*)$ /folder/anotherPage$1 [R=301,L]
I also tried something like this:
RewriteCond %{REQUEST_URI} ^/page1$
RewriteRule ^(.*)$ /folder/newPage [R=301,L]
RewriteCond %{REQUEST_URI} ^/page1$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ /folder/anotherPage%1 [R=301,L]
Note that Rewrite mod is turned on on my apache.
Any idea how to make it work?
Check through the following:
RewriteEngine On is present before all the rules
AllowOverride directive allows your htaccess files to load
htaccess receives URLs without the leading slashes, unlike server config files, or VHosts file.
.* matches 0 or more characters, thus %{QUERY_STRING} ^(.*)$ also matches empty query strings. Change this to .+.
Final rules should be:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^page1$ /folder/newPage [R=301,L]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^page1$ /folder/anotherPage?%1 [R=301,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]

Create RewriteRule Split query string in .htaccess for _escaped_fragment_

I am redirecting requests to snapshots directory for google hashbang.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /snapshots/%1? [NC,L]
So: http://site/?_escaped_fragment_=/detail/10
Go here: site/snapshots/detail/10
But I want to ignore the second params so
http://site/?_escaped_fragment_=/detail/10/12-12-2014
Should be redirect to the same url
site/snapshots/detail/10
Any idea how I should rewrite the rules?
Try changing this line:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
to
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?([^/]+(?:/[^/]+|))
So the regex will only attempt to capture as many as 2 virtual paths.

redirect from specific request

I have something like this:
RewriteCond %{QUERY_STRING} ^something=true$
RewriteRule ^$ http://www.a123dress.com/ [R=301,L]
I want redirect ALL (*) request which have "?something=true" to
http://www.a123dress.com/
It works for:
http://www.a123dress.com/?something=true
I want rewrite for example:
http://www.a123dress.com/test/?something=true
to:
http://www.a123dress.com/test/
so cut off ?something=true like above in first example
Your regex ^$ is just allowing home page / to be redirected. Change your rule to:
RewriteCond %{QUERY_STRING} ^something=true$ [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L,NE]

How to do a specific condition for escaped_fragment with rewrite rule in .htaccess

I have urls like that /#!/page1, I redirect them with :
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F(.*)$
RewriteRule ^$ /seo/%1.html [QSA,L]
So /#!/page1 read as /_escaped_fragment_=%2Fpage1 is redirected to /seo/page1.html
It's work perfectly however I want to redirect the home /#!/ to /seo/index.html which actually redirect to /seo/.html
How can I do that?
Thanks
I fixed it with:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F$
RewriteRule ^$ /seo/index.html [QSA,L]
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F(.*)$
RewriteRule ^$ /seo/%1.html [QSA,L]