htacess Proxy Redirect /a to /b, 301 redirect of /b to /a - apache

I have website built on a bespoke centralised legacy CMS.
It has a section which works under the URL /database
For one site I'd like to change that to use /blog and it still needs to tell the CMS that it's database.
So for .htaccess I have
RewriteRule ^blog$ /database [P,L]
RewriteRule ^blog/(.*)$ /database/$1 [P,L]
Which works great. But I also need to tell google that the site has changed and not to look at the old one.
So I need /database to 301 to /blog.
So:
RewriteRule ^database$ /blog [L,R=301]
I've tested this with http://htaccess.madewithlove.be and it says it's fine, but it's causing an infinite loop in the browser.
To be honest I feel like part of the problem is that I can't really express the question very well, especially the title, so any feedback on that would be appreciated.
Here's the rest of the file just in case that's relevant
# redirect trailing slashes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.+)?fromMobile=true$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
#uk.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^iflg\.uk\.com$ [NC]
RewriteRule ^(.*)$ http://www.iflg.uk.com/$1 [R=301,L]
RewriteRule ^blog$ /database [P,L]
RewriteRule ^blog/(.*)$ /database/$1 [P,L]
#RewriteCond %{REMOTE_ADDR} !=localhost
#RewriteRule ^database$ /blog [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~iflg/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~iflg/index.php
</IfModule>

You need to exempt the second rule to prevent the proxy server from being redirected too.
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteRule ^database$ /blog [L,R=301]
Do not rely on that htaccess tester, it is too simplistic.

Related

mod_rewrite rule for "www to non www" doesn't redirect sub-pages

I have made a thorough search before I have asked this question here. Please hear me out:
I am trying to redirect my blog from www to non www and it doesn't redirect any sub-pages. I have an http > https redirect in place as well and it works perfectly for both domain as well as the sub-pages. Here are the rules I have in my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^^rcp-pep-ipn //?rcp-pep-listener=IPN [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
# END WordPress
I'd really appreciate an explanation if I am doing anything wrong here. I have literally pulled my hair since I have used the exact same code (from the second RewriteBase /) for all other sites and it worked flawlessly.
You should bring those protocol checking conditions to the beginning. You have some problems within the rules too. Try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,NE,L]
RewriteRule ^rcp-pep-ipn /?rcp-pep-listener=IPN [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Non-www RewriteRule not working on request URI

I need a website to have all URLs NOT have www in front of it. For this, I have found some useful bits of code here that have helped me out a lot. However, nowhere can I find how to properly do it for ALL request URIs. Currently it only works on the domains homepage.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^(.*)$ https://domain.nl%{REQUEST_URI} [L,R=301]
</IfModule>
# END WordPress
Right now www.domain.nl gets rewritten into domain.nl. However, any other like www.domain.nl/anything_else do not get rewritten (also tried adding a / before the %{REQUEST_URI}, did not work).
Any help is appreciated!
With www rule before:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^ https://domain.nl%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

.htaccess ensure www redirect incorrect

I am trying to ensure that urls always have www in front of them for canonicalization reasons.
Unfortunately when I put the following url in:
http://website.net/handbags/1/12/this-is-some-text
It redirects me here:
http://www.website.net/?controller=handbags&path=1/12/this-is-some-text
I would like to add it works fine when using:
http://www.website.net/handbags/1/12/this-is-some-text
I want it to redirect me here...
http://www.website.net/handbags/1/12/this-is-some-text
I'm not sure what could be causing this error. Here is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# CORE REDIRECT
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{7})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>
You can do what I've done above; however, it's just going to redirect to http://www.website.net/handbags/1/12/this-is-some-text and then it's immediately going to hit the "CORE REDIRECT" rule and send you to http://www.website.net/?controller=handbags&path=1/12/this-is-some-text.
Is there a reason you don't want it to redirect to "?controller=handbags" when they go to www?
UPDATE
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
Rewritecond %{HTTP_HOST} !^www\.website\.net
RewriteRule ^(.*)$ http://www.website.net/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>
Whenever your # CORE REDIRECT matches, the other rules are not even checked, as the core redirect rule has the Lflag set. That ones tells Apache to stop trying other rules. Remove that flag or change the rule. ;)

How to redirect specific links to https:// everything else to http:// using .htaccess?

I am trying to use htaccess to automatically direct all requests to certain pages to https and everything else to http://
Here is the code I've got the code to force ssl working below
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-homebuyers-guide https://domain.com/va-homebuyers-guide/ [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-purchase-request https://domain.com/va-purchase-request/ [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-streamline-refinance https://domain.com/va-streamline-refinance/ [R=301,L]
This is working as expected and redirects to the ssl versions of those pages. I then tried adding the following to redirect everything else to http:
RewriteCond %{SERVER_PORT} !^80
RewriteCond %{REQUEST_URI} !^va-homebuyers-guide$
RewriteCond %{REQUEST_URI} !^va-purchase-request$
RewriteCond %{REQUEST_URI} !^va-streamline-refinance$
RewriteRule ^(.*)$ http://domain.com/$1 [R,L]
This code results in a redirect loop when going to /va-purchase-request /va-homebuyers-guide and /va-streamline-refinance and does not redirect the other pages at all.
I'm totally stuck with this and any help would be massively appreciated!
Edit:
I also have this code in the .htaccess file added by wordpress... Could it be interfering with the other redirects?
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Replace your .htaccess code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
RewriteCond %{HTTPS} on
RewriteRule (?!^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$)^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

Apache mod rewrite .htaccess problem

My site is a php based site, but I've added wordpress in a /blog/ folder. The .htaccess file below should allow the /blog/ folder to be accessed, but I get a 404 error saying that blog.php doesn't exist.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !\.(gif|jpg|png)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*)/$ /$1_$2.php [L]
RewriteRule ^(.*)/$ /$1.php [L]
</IfModule>
Anybody able to help at all?
The last RewriteRule is redirecting your request to /blog/ to index.php, you should add a RewriteCond to check if the request is on the blog folder.
RewriteCond %{REQUEST_URI} !^/blog/.*
I managed this using the code below, for some reason the conditionals that were suggested don't work (I HATE .htaccess)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(blog) - [L]
RewriteCond %{SCRIPT_FILENAME} !\.(gif|jpg|png)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*)/$ /$1_$2.php [L]
RewriteRule ^(.*)/$ /$1.php [L]
</IfModule>
Adding
RewriteRule ^(blog) - [L]
to public_html/.htaccess after
RewriteEngine On
worked for me as well on a fresh Wordpress installation with Fantastico on a Hostgator account, with a blog.example.com subdomain.