.htaccess: Allow specific URL - .htaccess

I have an angularJS site and the .htaccess file have been configured to redirect all traffic from mysite.se/* to mysite.se/index.html.
It also configured with prerender.io to let search engines to access the site.
Question: I want to add a blog to blog.mysite.se, and the problem is of course that it will be redirected to mysite.se/index.html. What do I need to add to the .htaccess file to let through the blog.mysite.se?
Thanks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^MYSITE\.se$ [NC]
RewriteRule ^.*$ http://www.MYSITE.se%{REQUEST_URI} [R=301,L]
# If requested resource exists as a file or directory
# (REQUEST_FILENAME is only relative in virtualhost context, so not usable)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
# Go to it as is
RewriteRule ^ - [L]
# If non existent
# If path ends with / and is not just a single /, redirect to without the trailing /
RewriteCond %{REQUEST_URI} ^.*/$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)/$ $1 [R,QSA,L]
# Handle Prerender.io
RequestHeader set X-Prerender-Token "kEJ0CC1gMnj6V0J4u8xu"
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
# Proxy the request
RewriteRule ^(.*)$ http://service.prerender.io/http://%{HTTP_HOST}$1 [P,L]
# If non existent
# Accept everything on index.html
RewriteRule ^ /index.html

Insert this rule just below RewriteEngine On line to skip blog subdomain from current rule:
RewriteEngine On
RewriteCond %{HTTP_HOST ^blog\. [NC]
RewriteRule ^ - [L]
# rest of rules here

Related

Redirect in htaccess to folder and https

Using htaccess I want to force http to https and at the same time redirect to www. and to the 'public' folder where my application files are.
The result should be that http://example.com should redirect to https://www.example.com/public/
To redirect to https and www I manage by using:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
To redirect to the 'public' folder I manage by using:
ReWriteCond %{REQUEST_URI} !^public/
ReWriteRule ^(.*)$ public/$1 [L]
Now I need to combine these but my many attempts did not result in a working redirect and I would need some advise on this.
EDIT:
In the folder public is another htaccess file:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
<<
ReWriteCond %{REQUEST_URI} !^public/
ReWriteRule ^(.*)$ public/$1 [L]
The REQUEST_URI server variable starts with a slash, so the condition above will never match. For example:
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule (.*) public/$1 [L]
However, assuming you have another .htaccess file in the /public subdirectory I would write this block like the following instead:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) public/$1 [L]
This has the added benefit of allowing a public path segment at the start of your URLs (if you wish). eg. The URL /public/foo would internally rewrite to /public/public/foo without creating a rewrite loop.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
You are missing an OR flag on the first condition. For example:
RewriteCond %{HTTPS} off [OR]
:
Without that, it will fail to redirect http://www.example.com/ to HTTPS.
Just to clarify, the canonical redirect should go before the internal rewrite to the public directory.
UPDATE:
EDIT: In the folder public is another htaccess file:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
The substitution string on the last RewriteRule directive is incorrect. You need to remove the slash prefix from /index.php. It should just read index.php. Otherwise it is going to rewrite the request back to the document root (filesystem path) and result in either an endless loop or a 404 (depending on the parent directives). By changing it to index.php (no slash) it is now relative to the /public directory (the directory that contains the .htaccess file), instead of the document root.
You can also simplify the RewriteRule pattern and the NC flag is not required here. So, in summary, the /public/.htaccess file should read:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.php [L]

Redirect if file does not exists to root

I have a super basic redirect request I like to get done, but i mess it up every time i give it a try, the best so far results in a double www.www redirect (http://www.www.domain.tld/)
My .htaccess has 'two' rules i like to get checked and done.
1st: Redirect all non existent files to the root folder
2nd: Add www. if domain requested as non-www
# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) http://www.%{HTTP_HOST}/? [L]
#Rewrite for non www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/? [R=301,L]
I have changed the first Ruleset, but never worked out
No. 1
# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) http://%{HTTP_HOST}/? [L]
Ended with a redirectloop
No. 2
# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) /? [L]
Ended like no rule was even given.. so an error 404 site with the unchanged URL http://www.domain.tld/123456789.html
Where am i going the wrong way?
You cannot use http:// while doing silent rewrite otherwise mod_rewrite will convert that to an external redirect. Have your rules like this:
DirectoryIndex index.php
RewriteEngine On
#Rewrite for non www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# If requested resource is not a file or directory then redirect to /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [L,R=302]
Make sure to clear your browser cache before testing this.

Redirect a specifc but wild card * URL (with folder(s)) to new domain (same structure)

I need to redirect a specific URL (with structure) to a the same URL(s) using a new domain, but not other URLS.
domainA.com/company/careers*
domainB.com/company/careers*
The reason for this is a 3rd party vendor supplying a jquery based iframe app that perfoms a referrer check before loading.
I realize there is a bigger seo/duplicate content issue that needs to be addressed, but there is a lot of additional work that needs to happen before domainA.com is fully redirected to domainB.com so for now, Its only the "career" section.
The site is using IIS6 with HeliconTech's ISAP ReWrite3
http://www.helicontech.com/isapi_rewrite/doc/introduct.htm
Current Rules:
# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.59
<VirtualHost www.domainA.com www.domainB.com>
RewriteEngine On
#RewriteBase /
#RewriteRule ^pubs/(.+)\.pdf$ /404/?pub=$1.pdf [NC,R=301,L]
# Send away some bots
RewriteCond %{HTTP:User-Agent} (?:YodaoBot|Yeti|ZmEu|Morfeus\Scanner) [NC]
RewriteRule .? - [F]
# Ignore dirctories from FarCry Friendly URL processing
RewriteCond %{REQUEST_URI} !(^/measureone|^/blog|^/demo|^/_dev)($|/)
RewriteRule ^([a-zA-Z0-9\/\-\%:\[\]\{\}\|\;\<\>\?\,\*\!\#\#\$\ \(\)\^_`~]*)$ /index.cfm?furl=$1 [L,PT,QSA]
RewriteCond %{REQUEST_URI} ^/company/careers [NC]
RewriteRule ^company/careers/?(.*)$ http://www.domainname.com/company/careers/$1 [R=301,L]
# Allow CFFileServlet requests
RewriteCond %{REQUEST_URI} !(?i)^[\\/]CFFileServlet
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /blog/index.php [L]
</VirtualHost>
<VirtualHost blog.domainA.com>
RewriteEngine On
#redirect old blog.domainA.com/* posts to www.domainB.com/blog/*
RewriteCond %{HTTP_HOST} ^blog.domainA\.com [nc]
RewriteRule (.*) http://www.domainB.com/blog$1 [R=301,L]
</VirtualHost>
It seems that "RewriteBase /blog/" line corrupts your "careers" rule as it implies that the request should be domainA.com/blog/company/careers*
Please consider having it like this:
<VirtualHost www.domainA.com www.domainB.com>
RewriteEngine On
RewriteBase /
#RewriteRule ^pubs/(.+)\.pdf$ /404/?pub=$1.pdf [NC,R=301,L]
# Send away some bots
RewriteCond %{HTTP:User-Agent} (?:YodaoBot|Yeti|ZmEu|Morfeus\Fucking\Scanner) [NC]
RewriteRule .? - [F]
# Ignore dirctories from FarCry Friendly URL processing
RewriteCond %{REQUEST_URI} !(^/measureone|^/blog|^/demo|^/_dev)($|/)
RewriteRule ^([a-zA-Z0-9\/\-\%:\[\]\{\}\|\;\<\>\?\,\*\!\#\#\$\ \(\)\^_`~]*)$ /index.cfm?furl=$1 [L,PT,QSA]
RewriteCond %{REQUEST_URI} ^/company/careers [NC]
RewriteRule ^company/careers/?(.*)$ http://www.domainname.com/company/careers/$1 [R=301,L]
# Allow CFFileServlet requests
RewriteCond %{REQUEST_URI} !(?i)^[\\/]CFFileServlet
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/.* /blog/index.php [L]
</VirtualHost>
If you still have issues, enable logging in httpd.conf by putting
RewriteLogLevel 9
and check how your request is processed in rewrite.log.
Just check to see if the request starts with /company/careers
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/company/careers [NC]
RwriteRule ^company/careers/?(.*)$ http://domainB.com/company/careers/$1 [R=301,L]
See if that works for you.

http to https htaccess redirect loop error

I have a site which I am trying to completely redirect from http to https.
I need to redirect any user that arrives at the site via http to the same page on https.
The site is hosted on a provider's shared hosting. Since the domain's SSL certificate was installed, a new IP address has been assigned to the domain.
I have been advised that the following code should be added to my .htaccess file. There are also some rules which handle SEO friendly URLS.
RewriteEngine On
RewriteBase /
# enforce https and www
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,NE,R=302]
# skip further rules for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal rewrite rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ page.php?venue=$1&artist=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ page.php?venue=$1 [L,QSA]
For some reason, the addition of this code to the .htaccess file, results in a redirection loop error. I have tried variants of the RewriteCond %{SERVER_PORT} !=443 line as advised on other Stack Overflow posts, but nothing seems to work.
Does anyone have a solution?
I have been given this code from my service provider which seems to do the trick using php
<?php
if ($_SERVER['HTTP_X_FORWARDED_SSL'] == '1') { } else { $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; header("Location: $redirect"); } ?>
I can add the following for permanent redirection
header("HTTP/1.1 301 Moved Permanently");
You can use this code in your root .htaccess:
RewriteEngine On
RewriteBase /
# enforce https and www
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,NE,R=302]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
# skip further rules for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal rewrite rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ page.php?venue=$1&artist=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ page.php?venue=$1 [L,QSA]

How do I setup an .htaccess file to exclude a subdirectory?

I currently have a website setup in the following way:
example.com is the root domain
The .htaccess file redirects all requests to the root URL to blog.example.com
This is because the blog subdomain is a mirror of the main domain and is what will be used moving forward.
The problem is that there's a second domain: product.com that is pointing to example.com/product.
As the .htaccess file is configured, all incoming requests are mapped to blog.example.com.
This means that going to product.com will result in blog.example.com/product in which the ideal scenario is to have example.com/product and all other incoming requests be redirected to blog.example.com.
A copy of my .htaccess file is below:
# Block access to the root site
RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
# Whitelist specific areas of the root site
# e.g category slugs or page slugs you want to remain viewable
RewriteCond %{REQUEST_URI} !/blog.* [NC]
RewriteCond %{THE_REQUEST} !/blog.* [NC]
# Set like-to-like redirect URL for anything not whitelisted
RewriteRule (.*) http://blog\.example\.com/$1 [R=301,L]
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
How can I construct my rules in the following way:
product.com redirects to example.com/product
example.com redirects to blog.example.com
This should work:
RewriteEngine On
# redirect anything that is different than /blog and /product to blog.example.com
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !/blog.* [NC]
RewriteCond %{REQUEST_URI} !/product.* [NC]
RewriteRule ^(.*)$ http://blog.example.com/$1 [R=301,L]

Resources