Apache Rewrite Conditions - apache

I am currently trying to redirect all frontend pages on a Magento install to a specific page, however when I do this, I can no longer gain access to the Magento back office. I have tried the following, but it doesnt appear to work, it still redirects everything.
Any help would be much appreciated.
RewriteCond %{REQUEST_URI} !^/index.php/admin/
RewriteRule ^(.*) new-url [R=301,QSA,L]

Can you try this as your first rule:
RewriteCond %{REQUEST_URI} !/admin/
RewriteRule ^ new-url [R=301,L]
Make sure to test in a new browser.

Related

URL not re-writing as expected in apache XAMPP

The requested url is http://localhost/views/items?item=abc I want to rewrite it to http://localhost/views/myfile.php?reqtype=items&item=abc please note that the last keyword of url before query items I want to move to query section as reqtype=items to get this I wrote below code in .htaccess
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^views/(.*)$ views/myfile.php?reqtype=$1&%1 [L,NE]
This code is working well when I tests here verified its output (from this link) by directly pasting in browser as URL worked well. but with xampp (my version is 7.1.28) it is not working. Remember url-rewriting is working for other type of conditions ( means apache configuration is correct and working ) but this particular example is not working, Any solution will be appreciated, Thanks.
Could you please try following, written and tested with shown samples. Please make sure you clear your browser cache before testing your URLs. NOTE that here condition RewriteCond %{ENV:REDIRECT_STATUS} ^$ is very important to stop looping else it will be keep redirecting. I have checked these rules by curl command and they work fine.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/views/(items)\?([^\s]*)\s [NC]
RewriteRule ^(.*)$ views/myfile.php?reqtype=%1&%2 [NE,L]

.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]

Which way do htaccess rules cascade?

I'm trying to archive an old website behind a /v4 directory on the new website. I was hoping that the below .htaccess was going to redirect any page from the example website to its corresponding page archived under the new domain. However, when I just tested the site it appears that example.org.au/contact.asp just went to newdomain.com/v4/ instead of `newdomain.com/v4/contact.html
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.org\.au$
RewriteRule ^(.*)\.asp$ https://newdomain.com/v4/$1.html [R=302,NC,L]
RewriteRule (.*) https://newdomain.com/v4/ [R=302,NC,L]
The second RewriteRule is there for when people just go to the root domain but is it overriding everything?
Do it like this, and also your first rule was set to ignore example.org.au so would never fire. This will process any host served by the site. If that's not what you want let me know:
RewriteEngine On
RewriteRule ^(.*)\.asp$ https://newdomain.com/v4/$1.html [R=302,NC,L]
RewriteRule ^$ https://newdomain.com/v4/ [R=302,L]

apache RewriteCond doesn't match empty http_referer

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.

Quick mod_rewrite help on GoDaddy

This htaccess works fine locally, but on GoDaddy the URL isn't caught by the rewrite engine.
RewriteEngine on
RewriteRule ^products/amsoil/(.*)/$ /products.php?amsoil=$1 [L]
RewriteCond %{HTTP_HOST} ^somedomain.com
RewriteRule (.*) http://www.somedomain.com/$1 [R=301,L]
This worked until a few days ago. Basicallywww.somedomain.com/products/amsoil/this-product/ should forward to www.somedomain.com/products.php?amsoil=this-product ....did work, and still works locally however now I just get a 404 error on www.somedomain.com/products/amsoil/this-product/
Any ideas?
I verified that the following script works on our current hosting plans:
RewriteEngine on
rewritecond %{http_host} ^coolexample.com [nc]
rewriterule ^(.*)$ http://www.coolexample.com/$1 [r=301,nc]
There is a possibility that you are on an older version of our hosting plans. If that is the case, you may want to consider upgrading. Check out http://x.co/Zecq for a how-to on upgrading. There is also a link to the 4GH FAQ. Please review that before upgrading to avoid unwanted surprises.