How can I allow specific URL in htaccess? - apache

My root .htaccess in a Magento store ensures that any visits not written as (www.)domain.com/correct will go to our 404 (where "correct" is an existing sub-directory). (www.)domain.com/admin is also allowed through.
I now need to also allow some other URLs that do not follow the (www.)domain.com/correct form.
Presently the code in the .htaccess is:
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^$ go404 [R=301,L]
How can I make the following go through as well (ie NOT go404 on me)?
www.domain.com/correct/index.php/folder1/folder2/folder3/

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]

Force HTTPS and WWW in .htaccess

I start reading about a similar topic at this page .htaccess - how to force "www." in a generic way? and the solution was not, well almost what I am looking to do.
The problem : I need the user to be on HTTPS and on WWW to make my application working properly. But if some one click on a html link like:
www.example.com
The user will fall on my website with this :
https://www.www.example.com/
Here is my current .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://www.%{SERVER_NAME}/$1 [R=301,L]
</IfModule>
Is there any way to detect that the user already entered the WWW or is there a best practice to get the result I am looking for?
Thank you.
You are getting this behavior because http -> https rule is adding www\. in target URL without checking if URL is already starting with www.
You should replace both of your rules with this single rule and as a bonus avoid multiple redirects:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

.htaccess file making new domain redirect to my original domain

To give a little background that may help understand this. I only had one domain on my host's server initially. I just added a new domain and it was redirecting to my first site.
I had to create an .htaccess file redirect my first site so that it would have my SSL on every page and so that it took the www. away from the sites name. The www. was causing issues with sessions when a customer would login.
So my code was like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
The tech support of my host company said that this line was making my new site redirect to my old site:
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
How can I make my code work so that is still has the exact same functionality of what it was doing and not interfere with my new domain and make it re-route to my old site?
Why not just exclude the new site, if I am understanding correctly?
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?newsite\.com [NC]
RewriteCond %{HTTP_HOST} ^www\.oldsite\.com [NC]
RewriteRule ^(.*)$ https://oldsite.com/$1 [R=301,NE,L]
RewriteCond %{HTTPS} ^off
RewriteCond %{HTTP_HOST} !^(www\.)?newsite\.com [NC]
RewriteRule ^(.*) https://oldsite.com/$1 [R=301,NE,L]
Alternatively maybe you can just tell it to not do anything based on the host. Try putting this at the top of your rules.
RewriteCond %{HTTP_HOST} ^(www\.)?newsite\.com [NC]
RewriteRule ^ - [L]
Note I haven't tested this.

Need a htaccess redirect rule to ignore a certain folder

I'm redirecting my .com website to .net using this
RewriteCond %{HTTP_HOST} ^cartoonizemypet\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.cartoonizemypet\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.cartoonizemypet\.net\/$1" [R=302,L]
Which works (huzzah!) The problem is I need to also exclude a single folder and all of it's contents: http://www.cartoonizemypet.com/catoonizer
I've been messing around with it all day, trying to adapt other peoples from here, but I just keep breaking the site. I'm afraid I just don't know that much about rewrite rules.
Does anyone know how I can make it work?
Try:
RewriteCond %{REQUEST_URI} !^/catoonizer
RewriteCond %{HTTP_HOST} ^cartoonizemypet\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.cartoonizemypet\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.cartoonizemypet\.net\/$1" [R=302,L]
Another way of doing it:
# Turn the engine on if it's not already
RewriteEngine On
# Check to make sure we're on the right host (www is optional)
RewriteCond %{HTTP_HOST} ^(www\.)?cartoonizemypet\.com$ [NC]
# Check to make sure we're not at the catoonizer URI (I assume that's a misspelling
# but it's what was in the example).
RewriteCond %{REQUEST_URI} !^(/catoonizer) [NC]
# If all conditions are met, redirect as 302.
RewriteRule ^(.*)$ http://www.cartoonizemypet.net/$1 [R=302,L]

Apache mod_rewrite accept language subdomain redirection

i have a multilingual site with 3 languages and i'm using the following rules to redirect requests to the right version of the website based in browser accept language.
#swedish
RewriteCond %{HTTP:Accept-Language} ^sv.*$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} !(^q\=) [NC]
RewriteRule ^(.*)$ /sv [L,R=301]
#norwegian bokmal
RewriteCond %{HTTP:Accept-Language} ^nb.*$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} !(^q\=) [NC]
RewriteRule ^(.*)$ /nb [L,R=301]
#norwegian
RewriteCond %{HTTP:Accept-Language} ^no.*$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} !(^q\=) [NC]
RewriteRule ^(.*)$ /nb [L,R=301]
#all others go to english
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} !(^q\=) [NC]
RewriteRule ^(.*)$ /en [L,R=301]
I need to upgrade this rules to do the same redirections keeping subdomains too
Example for a norwegian request:
www.domain.com/subdomain -> www.domain.com/nb/subdomain
How can i achieve this?
I have thank #faa for helping me out with the current rules.
UPDATE
Question Description
Regarding the pages vs subdomains issue in your comments, in this specific case is irrelevant. The fact is the incoming URL in your question is: www.domain.com/subdomain and, except for the name, /subdomain is clearly a page.
A real subdomain is indeed a domain that is part of another domain and the URL format that holds it, is something like subdomain.domain.com, so there was no confusion on my part.
What it is not clear enough, is how the redirection will be handled. According to your question, but replacing "subdomain" with the whole path, here are some examples:
www.domain.com/ should go to www.domain.com/LangCode/, (previous working redirection)
www.domain.com/page should go to www.domain.com/LangCode/page
www.domain.com/page1/page2/page3/etc/ should go to www.domain.com/LangCode/page1/page2/page3/etc. This possibility is not in the question, but eventually could be neded.
In those cases, the pages in the incoming and redirected URLs should have the same name, but, although in the incoming URL do not have to exist, in the redirected URL the pages MUST exist and so a loading default script (index.php or index.html, for example) to handle the request.
Which means, there has to be a script in each page subject to redirection. I would say at least 2 for each language.
As far a I understand, that's what the question and complementary comments indicate, but it seems it is not a practical approach.
Suggested Solution
A better approach could be a single script at the root folder that handles all requests. This is an idea that can be better described with examples:
www.domain.com/ always showing in the browser's address bar but going internally to
www.domain.com/lang_handler.php?lang=sv or
www.domain.com/page1/ always showing in the browser's address bar but going internally to www.domain.com/lang_handler.php?lang=sv&target_page1=page1
This can be achieved in .htaccess with mod_rewrite directives. Here is an example:
RewriteEngine On
RewriteBase /
# Set managed languages here, except default (en)
RewriteCond %{HTTP:Accept-Language} ^(sv|ne|no).*$ [NC]
# Replace the names of the script and the parameters in the next 2 lines
RewriteCond %{REQUEST_URI} !lang_handler\.php [NC]
RewriteRule ^([^/]+)?/?$ lang_handler.php?lang=%1&target_page1=$1 [L,QSA]
# If no match, set English
# Replace the names of the script and the parameters in the next 2 lines
RewriteCond %{REQUEST_URI} !lang_handler\.php [NC]
RewriteRule ^([^/]+)?/?$ lang_handler.php?lang=en&target_page1=$1 [L,QSA]
The above rule set maps silently
http://www.domain.com/ or http://www.domain.com/page1
To
http://www.domain.com/lang_handler.php?lang=LangCode&target_page1=page1
Where LangCode is sv ne no or en by default.
This example only works for 1 page or no page. Any number of pages can be handled though, but the rules have to be modified accordingly. More parameters and regex groups have to be added to the RewriteRules.
$1 should do the trick.
RewriteCond %{HTTP:Accept-Language} ^no.*$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} !(^q\=) [NC]
RewriteRule ^(.*)$ /nb/$1 [L,R=301]