Apache 2/mod_rewrite: setting a cookie, then testing for its presence and redirecting to a target when cookie not present - mod-rewrite

I am trying to get this working, using only Apache 2 directives and mod_rewrite.
RewriteEngine on
RewriteBase /
RewriteRule .* - [CO=setcookietest:1:example.com:60:/] [C]
RewriteCond %{HTTP_COOKIE} !^.*setcookietest.*$ [NC]
RewriteRule .* /cookies.html [NC,L]
This is not working, it results in an internal server error when hitting that last directive.
Basically, all I want to do is rewrite to a particular page if the server cannot set a test cookie, its value is irrelevant.
Thanks!

Rewrite flags are used inside [ and ] (a single pair of closing and opening square brackets ). The reason why your server returned a 500 error response is because you are using double flag delimiters [.....][...] in your first rule .
RewriteEngine on
RewriteBase /
RewriteRule .* - [CO=setcookietest:1:example.com:60:/,C]
RewriteCond %{HTTP_COOKIE} !^.*setcookietest.*$ [NC]
RewriteRule .* /cookies.html [NC,L]

Related

Redirect Subdomain to domain while preserving the path and query string

I have a subdomain and I want to redirect to the main domain (using .htaccess) like so:
https://abc.example.com I want to redirect it to https://www.example.com
https://abc.example.com/path/page-name to https://www.example.com/path/page-name
https://abc.example.com/path/page-name?test=12&test1=12 to https://www.example.com/path/page-name?test=12&test1=12
Please suggest how I can do it.
I have already tried the below solution but its not working.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteRule ^ http://www.example.com/suberror [L,R]
I am using Laravel.
At this line :
RewriteRule ^ http://www.example.com/suberror [L,R]
There is no pattern means , regex checked against requested URI .
Change it to this :
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R]
This part ^(.*)$ is pattern and it will be presented in substitution by this $1
If it is Ok , Change [L,R] to [L,R=301] to be permanent redirection because R alone means R=302 which is temporary .
Assumptions:
You have 1 subdomain (as stated in your example)
The subdomain and main domain point to the same area on the filesystem (they share a common root).
The try something like the following at the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc\.example\.com [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R,L]
The URL-path from the request is held in the REQUEST_URI server variable. The query string from the request is passed through to the substitution (target) without any additional work.

Rewriterule shows wrong URL

I have a webapplication working on:
next3.info:83
I want to go there with:
weburen.next3.nl so I used this code:
RewriteEngine on
RewriteCond %{HTTP_HOST} =weburen.next3.nl
RewriteCond %{SERVER_PORT} !^83
RewriteRule ^(.*) http://next3.info:83/ [L]
It goes to:
next3.info:83 so the redirect works
But I want to see:
weburen.next3.nl not next3.info:83
How can I make this work?
When you use absolute URL for RewriteRule substitution, it will always force external redirect to new hostname. If you want to preserve original hostname, you have to configure as proxy by enabling mod_proxy and mod_proxy_http modules, then add [P] flag to your RewriteRule. As example:
RewriteRule ^(.*) http://next3.info:83/ [P]
[P] flag is documented in the link:
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p

Mod-Rewrite - Need two different redirection rules

I would like to have two different redirects; one redirection if a user accesses an index.php-file on my apache-server and one redirection after a user enters a particular url.
So my index.php file lies in "/client/frontend/questionnaire"-folder on my apache. If a user enters "www.test-domain.com" he or she should be redirected to the index.php - file on the server.
The second redirection should be processed if a user enters "www.test-domain.com/news". Then he or she should be redirected to "www.test-domain.com/client/frontend/app/index.php/article-one".
I managed to create the first rewrite rule like this:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^$ client/frontend/questionnaire/index.php [L]
But I do not know how to create the second rule.
Any help is very appreciated.
Thanks!
You have to use multiple RewriteCond+RewriteRule. See the mod_rewrite introduction and then the reference documentation (all is explain).
Just the RewriteRule actually rewrite the current request. To execute it, all RewriteCond before must be true.
Samples from the documentation:
RewriteCond %{REMOTE_ADDR} ^10\.2\.
RewriteRule (.*) http://intranet.example.com$1
RewriteCond %{REMOTE_HOST} ^host1 [OR]
RewriteCond %{REMOTE_HOST} ^host2 [OR]
RewriteCond %{REMOTE_HOST} ^host3
RewriteRule ...some special stuff for any of these hosts...
RewriteCond %{HTTP_USER_AGENT} (iPhone|Blackberry|Android)
RewriteRule ^/$ /homepage.mobile.html [L]

mod_rewrite secure redirect

Struggling with mod_rewrite trying to redirect a non-secure page to a secure one. This works:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} (help/returns)
RewriteRule .? https://mysite.localhost/%1/ [R=301,L]
But this doesn't:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP:Host} (mysite.localhost|mylivesite.com)
RewriteCond %{REQUEST_URI} (help/returns)
RewriteRule .? https://%1/%2/ [R=301,L]
The URL it tries to give me is https://help/returns//
I can't seem to get the HTTP:host into the final RewriteRule line.
I need the host in there so I can use the same file for local dev and live deployment.
Most grateful for any input.
You can use this rule:
RewriteCond %{HTTPS} =off
RewriteRule ^help/returns https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L]
This rule will redirect all requests to http://example.com/help/returns to a secure (HTTPS) location: https://example.com/help/returns -- it will preserve full URL path + query string. You have too many conditions, rule becomes complex which is not a good thing when your server is REALLY busy (regular expressions are expensive).
I have replaced %{SERVER_PORT} 80 by more proper %{HTTPS} =off (this especially useful if your site is run on non-default port, which is 80).
I have also removed HTTP_HOST matching part -- you don't really need it unless you have more than one domain name/subdomain bound to the same site. In case if you need this condition just add this line after 1st line: RewriteCond %{HTTP_HOST} ^(mysite.localhost|mylivesite.com)

mod_rewrite "too many redirects" problem

Trying,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]
RewriteCond %{HTTP_HOST} ^(.*)dev\.example\.edu$ [NC]
RewriteRule ^/test(.*)$ http://dev.example.edu/test/index.php/test$1 [NC]
</IfModule>
on an apache 2.2 server, to get this rewrite working to hide the "index.php/test" part of the path.
everything i've tried either loops the url part (index.php/test) within the address bar or gives a "too many redirects" error.
i suspect that the "test" part of the equation being on both sides is throwing it off but am not sure how to get it to work.
i just want:
dev.example.edu/test/index.php/test*
to rewrite to:
dev.example.edu/test/*
thanks
You need to exclude the destination path to avoid an infinite recursion:
RewriteCond %{HTTP_HOST} ^(.*)dev\.example\.com$ [NC]
RewriteCond $1 !^/index\.php/test/
RewriteRule ^/test/(.*)$ http://dev.example.com/test/index.php/test/$1 [NC]
Here the match of the first grouping ($1) is checked not to match ^/index\.php/test/.
But if you want to rewrite /test/index.php/test/… to /test/…, you will rather need this rule:
RewriteCond %{HTTP_HOST} ^(.*)dev\.example\.com$ [NC]
RewriteRule ^/index\.php/test/(.*)$ http://dev.example.com/test/$1 [NC]
Per Jim at webmasterworld (thanks!)
"The [P] flag invokes a reverse-proxy request to the server at the designated URL; That is, it opens a new out-going HTTP connection and sends a request to that server. So at the very least, your configuration is twice as slow as it should be, just using the original working rule, because your server is sending itself a new request via HTTP instead of just serving the content from a non-default-mapped filepath.
It seems to me that all that's needed is an internal rewrite, so that requests for the resource at URL
http://dev.example.edu/distribution/ are served with the content generated by the script at the server filepath /distribution/index.php/distribution/"
RewriteEngine on
#
# Return 403-Forbidden response to TRACE requests
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]
#
# Internally rewrite requests for URL-path /recreation/<anything>
# to filepath /eel/index.php/recreation/<anything>
RewriteCond %{HTTP_HOST} ^dev\.example\.edu [NC]
RewriteRule ^/recreation/(.*)$ /ee1/index.php/recreation/$1 [L]
#
# Internally rewrite requests for URL-path /distribution/<anything>
# to filepath /distribution/index.php/distribution/<anything>
RewriteCond %{HTTP_HOST} ^dev\.example\.edu [NC]
RewriteRule ^/distribution/(.*)$ /distribution/index.php/distribution/$1 [L]
So I think I was just making it more complicated than it had to be. I've removed the P flag and removed the full server address from the rewriterule.

Resources