For a multi-langue site I have used the following code to automatically redirect people to the right page. This works great, but does not include stylesheets and other files.
The following code in .htaccess turns www.site.com to www.site.com/fr if a visitor is from France. But it also redirects stylesheet and other files.
RewriteCond %{HTTP:Accept-Language} ^(nl.*) [NC]
RewriteCond %{REQUEST_URI} !(^/nl/.*) [NC]
RewriteRule ^(.*)$ /nl/$1 [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^(fr.*) [NC]
RewriteCond %{REQUEST_URI} !(^/fr/.*) [NC]
RewriteRule ^(.*)$ /fr/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(^/.*) [NC]
RewriteRule ^(.*)$ /$1 [L,R=301]
www.site.com/css/style.css turns into www.site.com/fr/css/style.css which doesn’t exist
www.site.com/js/file.js turns into www.site.com/fr/js/file.js which doesn’t exist
Is there a way to create an auto redirect, but exclude whole subdirectories? For example, exclude /css/. and /js/.
You can use
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
so that requests to files and directories that exist will not be rewritten
Related
I have the following in my htaccess file:
# drop tags
#RewriteCond %{THE_REQUEST} (.*)designs/(.*)/?tag=shirts [NC]
#RewriteRule .* /designs/%2/ [R=301,L]
RewriteCond %{THE_REQUEST} (.*)designs/([^?]+)\?tag=[^&]* [NC]
RewriteRule .* /designs/%2? [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/designs/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^designs/.*$ /$0? [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^mfp= [NC]
RewriteRule ^designs/ %{REQUEST_URI}? [L,NC,R=301,NE]
My theme supports a blog but after enabling it (it defaults to example.com/blog/), when I click on the blog link in my menu, it takes me to a page that says "There is no product that matches the search criteria". When I remove the htaccess rules listed above, the blog page (which contains the article listings) works fine so it is definitely that.
How can I exclude the word "blog" from the htaccess rules so this issue disappears?
I managed to fix it after a couple hours of tinkering. The solution was:
# external redirect from actual URL to pretty one
RewriteCond %{REQUEST_URI} !^/blog($|/)$
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_URI} !^/blog($|/)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/designs/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
This solution also handles /blog (without the trailing /).
Hope that helps someone in the future.
So I'm having this website which is stored inside public_html/www/. Some other domains are stored in -for example- public_html/test/. In public_html I have a .htaccess file with a LOT of redirects in it, which all apply to this website inside /www/ folder. Inside the /www/ folder I have another .htaccess (I don't really know why, my colleague did this and he left me with this without information). Since I have no idea how .htaccess exactly works, I have no idea what to do and in which file.
What I need to do is to redirect domain.nl to https://www.domain.nl (some folders should be excluded, like you can see in the code below - emailtemplates, paymentupdate, autocode, nieuwsbrieven, etc.) But domain.nl is stored inside public_html/www/ folder, so it also needs a redirect to the subfolder (which should not be visible in web browser).
At this moment, in the root .htaccess file is the following:
# Use HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl [NC]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/emailtemplates
RewriteCond %{REQUEST_URI} !^/paymentupdate_ideal
RewriteCond %{REQUEST_URI} !^/autocode
RewriteCond %{REQUEST_URI} !^/nieuwsbrieven
RewriteCond %{REQUEST_URI} !^/bevestig-aanmelding-nieuwsbrief
RewriteCond %{REQUEST_URI} !^/bevestig-afspraak
RewriteCond %{REQUEST_URI} !^/images/personal
RewriteCond %{REQUEST_URI} !^/uploadify
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Link to subfolder
# RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]
# Non-www to www
#activate rewrite engine
#RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.nl
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Probably half of this code isn't even necessary, but I have absolutely no clue. The other .htaccess file, within the subfolder /www/, has the following text which I don't really understand (it contains more, but nothing relevant):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.domain.nl/$1/ [L,R=301]
I'm sorry if I sound stupid. I've tried many things copied from internet, but unfortunately everything has its concequences, which results in other problems with our website (links that don't work anymore, such as). I think the reason it doesn't work all the time, is because it's stored in the subfolder and I don't know how to combine all those lines in one like it would work.
Can anybody help me explain how to fix this and what is going wrong at this moment? Because the website is now not redirecting to www. It's just redirecting to https which gives me a security error at this moment (because the SSL is stored on www.). SUPER thanks in advance!
My problem was wrong order of the checks. First, I redirected to https://, but when I did not use www., it did not redirect to https://www OR it redirected to https://www.www. I fixed my problem by re-ordering my rules, like below:
# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Permanent redirect to https://
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/excluded_folder1/
RewriteCond %{REQUEST_URI} !^/excluded_folder2/
RewriteCond %{REQUEST_URI} !^/excluded_folder3/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Use subfolder for domain
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]
I've been trying several solutions in the internet and can't make the redirect voodoo to work.
Setup
My setup is a website build with jekyll, and I want to consume it as mydomain.com/~user.
The folder structure is
~user
- _site
- index.html
- folder1
- index.html
- folder2
- index.html
- other_stuff
Problem
I was able to redirect from mydomain.com/~user to mydomain.com/~user/_site. So, half way through. The problem came when I tried to mask and remove the _site from the URL again. And I hit a wall.
Basically I want to use: mydomain.com/~user and see that URL in the browser, while beeing served the page that lives in mydomain.com/~user/_site/index.html. And same goes for other links
Request See Served
======= ======= =======
mydomain.com/~user mydomain.com/~user mydomain.com/~user/_site/
mydomain.com/~user/folder1 mydomain.com/~user/folder1 mydomain.com/~user/_site/folder1
What I try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/~user/_site/?
RewriteRule ^(.*)$ /~user/_site/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule !^~user/_site(.*)$ /~user/_site$1 [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule ^~user/_site(.*)$ /~user$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule !^~user/_site(.*)$ /~user/_site$1 [L]
However, all these
Do not end with the desired hidden URL in the bar
Give an error, as some of the redirects are not allowed in the server
Partial solution
After tweaking the proposed solution, I was able to identify one variation that partially works
RewriteEngine On
RewriteBase /~user
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/_site/? [NC]
RewriteRule ^(.*)$ _site/$1 [L]
Note that in the last two line, i remove the ~user from the regex. This redirects all subfolders and pages, and allows other folders that are in ~user to be accessed.
However, the "home" mydomain.com/~user is the only one that is not redirected. As it exists the RewriteCond %{REQUEST_FILENAME} !-d doesn't allow it to be processed.
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^/?$ _site/ [L]
To redirect the home (~user) to mydomain/~user/_site, but it doesn't work. And tried variations of ^/?$ and ^$. But they don't work either.
Ideas?
You should try it this way.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/~user/_site/? [NC]
RewriteRule ^~user/(.*)$ /~user/_site/$1 [L]
I am trying to setup a codeigniter app to force HTTPS across all pages except one. However, I cannot get the rules to only redirect if the user is not on the page in question.
The page that should be excluded has the following URL's:
http://mydomain.com/kpi/reports/67
http://mydomain.com/kpi/reports/67/overview/2013-02-01/2013-02-28
http://mydomain.com/index.php?/kpi/reports/67
http://mydomain.com/index.php?/kpi/reports/67/overview/2013-02-01/2013-02-28
The number 67 and the dates can all change in the URL's above hence the user of regular expressions below.
I have tested the regular expressions and they seem to match the URL's fine. However, the htaccess just seems to redirect it to https:// anyway.
My .htaccess file is as follows...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Disallow access to system dir
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Disallow access to application dir
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Any help would be greatly appreciated!
Maybe this will do what you need:
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
Replace all lines between the comments.
How can I redirect requests to mydomain.tld/somepage.ext to mydomain.tld/mydomain/somepage.ext? I have subdomains like subdomain.mydomain.tld that I don't want to be affected by this.
I can't seem to get it to work right. I'm trying this:
RewriteEngine on
RewriteCond %{HTTP_HOST} mysite.com
RewriteCond %{REQUEST_URI} !^/mysite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /mysite/$1
But it isn't redirecting anything at all.
I also want to exclude one or two folders from this rule.
This seems to be working...
RewriteEngine on
RewriteCond %{HTTP_HOST} mysite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mysite/ # but I don't see why this line is needed when I've used [L] below
RewriteCond %{REQUEST_URI} !^/ignored_folder/
RewriteRule .* /mysite/$0 [QSA,L]
At least for now.