Im trying to make a redirect listen to a certain domain.
I have 3 domains;
- www.tocdesal.com
www.tocdesal.es
www.tocdesal.nl
Now i want achieve this..
www.tocdesal.com > www.tocdesal.com/en_GB/
www.tocdesal.nl > www.tocdesal.com/nl_NL/
www.tocdesal.es > www.tocdesal.com/es_ES/
Here is some code i use to manage multilanguage and ban people form certain folders.
I hope somebody can help me with this feature.
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# NON-WWW > WWW
#RewriteCond %{HTTP_HOST} ^www\.tocdesal\.nl [NC]
#RewriteRule (.*) http://www.tocdesal.nl/nl_NL/home/$1 [R=301,L]
# CUSTOM REWRITES
#Redirect 301 /[oldlink] [new-full-link]
# We dont want snooping people
Options -Indexes
#RewriteCond %{REQUEST_URI} ^framework.*
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
#RewriteBase /tvmoordrecht
# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(framework/modules|framework/coremodules|framework/cicore|application|assets|custommodules)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|css|js|images|img|fonts|upload|framework/assets/|themes|framework/apps/elfinder|dev-cmslemonupdater|cmslemonupdater)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
Insert these 3 rules right after RewriteEngine On line:
RewriteCond %{HTTP_HOST} ^(?:www\.)?tocdesal\.com$ [NC]
RewriteRule !^en_GB/ http://www.tocdesal.com/en_GB%{REQUEST_URI} [NE,R=301,L,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?tocdesal\.nl$ [NC]
RewriteRule ^ http://www.tocdesal.com/nl_NL%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?tocdesal\.es$ [NC]
RewriteRule ^ http://www.tocdesal.com/es_ES%{REQUEST_URI} [NE,R=301,L]
This is what i have now. non-www redirects to www for the standard domain (.com) the rest redirects to the language on what the domain is set to.
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# Redirects to the page with that language selected
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?tocdesal\.nl$ [NC]
RewriteRule ^ http://www.tocdesal.com/nl_NL%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?tocdesal\.es$ [NC]
RewriteRule ^ http://www.tocdesal.com/es_ES%{REQUEST_URI} [NE,R=301,L]
# CUSTOM REWRITES
#Redirect 301 /[oldlink] [new-full-link]
# We dont want snooping people
Options -Indexes
#RewriteCond %{REQUEST_URI} ^framework.*
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
#RewriteBase /tvmoordrecht
# Protect application and system files from being viewed when the index.php is mi
RewriteCond $1 ^(framework/modules|framework/coremodules|framework/cicore|applica
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|css|js|images|img|fonts|uplo
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
Related
we have the following .htaccess configuration:
RewriteEngine on
allow from all
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php
RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain1\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^/?$ "http\:\/\/www\.domain1\.com\/" [R=301,L]
Between other things we want to redirect all non-www URLs to the ones with the www.
Everything works fine, except for the pages where the URL is like this:
index?Form%5bplace%5d=Caribbean&Form%5bdestination%5d=Virgin+Islands&Form%5btype%5d=A
When we enter the URL without the www our redirect ends up with the following URL:
index?Form%255bplace%255d=Caribbean&Form%255bdestination%255d=Virgin+Islands&Form%255btype%255d=A
Which gives an 404 error because it is not recognized.
Any idea how to avoid this?
Replace your code with this:
allow from all
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# if a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php [L]
RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^domain1\.com$
RewriteRule ^/?$ http://www.domain1.com/ [R=301,L]
What's happening is that the % symbol is getting escaped to %25.
You can avoid this using the NE flag on your rules
Can anyone halp me with this .htaccess file, i've been trying to make an exception so i can access my subdomain which is
form.domain.com
i tried with
RewriteCond %{REQUEST_URI} !^/form/?$
and several other commands but with no luck
.htaccess :
#######################
# N - T H I N G ! #
#######################
# Apache options
Options +FollowSymLinks -Indexes
RewriteEngine on
# Allow only GET and POST verbs
RewriteCond %{REQUEST_METHOD} !^(GET|POST)$ [NC,OR]
# Ban Typical Vulnerability Scanners and others
# Kick out Script Kiddies
RewriteCond %{HTTP_USER_AGENT} ^(java|curl|wget).* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*(libwww-perl|curl|wget|python|nikto|wkito|pikto|scan|acunetix).* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*(winhttp|HTTrack|clshttp|archiver|loader|email|harvest|extract|grab|miner).* [NC,OR]
# Error Page
ErrorDocument 404 /404
# Redirect attachments
RewriteRule ^files/attachments/(.*)/(.*)/(.*)$ files/attachments/$1/$2/$3 [L]
# Redirect all requests to index.php
RewriteRule ^(.*)$ index.php [L,QSA]
To match a domain you should use %{HTTP_HOST} as in:
RewriteCond %{HTTP_HOST} ^form.domain.com$
RewriteRule ^.*$ - [L]
That says if the domain matches form.domain.com, allow the URL and stop processing rules.
See the Apache documentation for more details.
I have 2 domains pointing to the root of my site:
http://site1.com
http://site2.com
both point to http://dXXXXXXX.domain.com/
I have each site set up to look for its files in a subdirectory (of the same name) so
http://site1.com files are located in http://dXXXXXXX.domain.com/site1/
http://site2.com files are located in http://dXXXXXXX.domain.com/site2/
I would like to create a special subdomain for site1 (and possibly site2) that actually points to a folder in the main directory (a forum that both domains can share). I would like to mask the real url with the subdomain url like the following:
http://subdomain.site1.com masks http://dXXXXXXX.domain.com/shared_folder/ (not redirected)
my .htaccess file (in the topmost directory) looks like this:
Options +FollowSymLinks
RewriteEngine On
# Rewrite "www.domain.com -> domain.com"
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
# redirect site1.com to /site1 [folder]
# redirect site2.com to /site2 [folder]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^0-9]+)$ [NC]
RewriteRule ^(.*)$ /%1/$1 [QSA,L]
#i think this should work but it doesn't
#RewriteCond %{HTTP_HOST} ^subdomain\.site1\.com$ [NC]
#RewriteRule ^$ shared_folder [P]
also, if possible, I would like to redirect the absolute path to each domain to my preferred path as follows:
http://dXXXXXXX.domain.com/site1/ redirects to http://site1.com
http://dXXXXXXX.domain.com/site2/ redirects to http://site2.com
not sure that anyone would ever find those links, but I'd like to keep everything clean by not having multiple access points for the same piece of content (with the exception of intentionally shared css or forum software)
Thanks in advance for any help or insight you may have for me
I think I figured it out (as in it hasn't broken yet!)
Options +FollowSymLinks
RewriteEngine On
# Rewrite/Redirect "www.domain.com -> domain.com"
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Rewrite/Redirect "domain.com/foo -> domain.com/foo/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ /$1/ [R=301,L]
# Redirect/Pass through "subdomain.site1.com to /shared_folder/"
RewriteCond %{HTTP_HOST} ^subdomain\.([\w.]+)$ [NC]
RewriteRule ^(.*)$ /shared_folder/ [L]
# Redirect/Pass through site1.com to /site1 [folder]
# Redirect/Pass through site2.com to /site2 [folder]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^0-9]+)$ [NC]
RewriteRule ^(.*)$ /%1/$1 [QSA,L]
So this works, but I'm not sure about the placement of my rules: if it's better to tackle the special subdomain case before the domains themselves, as in the order I have now; or if RewriteRule ^(.*)$ /shared_folder/ [L]is better written as RewriteRule ^$ /shared_folder/ [L]...
If you have any insight as to good or better practices I would appreciate it, thanks.
I have:
RewriteCond %{HTTP_HOST} ^MYDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://www.MYDOMAIN.com/$1 [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/(user|admin|cart)
RewriteRule ^(.*)$ https://www.MYDOMAIN.com/$1 [R=301,L]
This effectively routes all /user and /admin and /cart paths to use SSL. My problem is the inverse. I need a rule that states that if you are NOT on those pages, that it redirects you to HTTP (no SSL).
How do I do that?
First thing clear your browser cache completely and restart your browser. Then replace your .htaccess with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^MYDOMAIN\.com$ [NC]
RewriteRule ^ http://www.MYDOMAIN.com%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} !=on
RewriteRule ^(user|admin|cart)(/.*|)$ https://www.MYDOMAIN.com%{REQUEST_URI} [R,L,NC]
RewriteCond %{HTTPS} =on
RewriteRule (?!^(user|admin|cart)(/.*|)$)^.*$ http://www.MYDOMAIN.com%{REQUEST_URI} [R,L,NC]
Just invert the condition!
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(user|admin|cart)
RewriteRule ^(.*)$ http://www.MYDOMAIN.com/$1 [R=301,L]
Does this work? I realize it might not, since the SETENV does not always get processed before the ReWriteRule, but hypothetically, this should handle all of your scenarios:
# Set Correct protocol var based on request:
## Default to http
SetEnv correct_protocol http
## Overwrite to https if secure area
RewriteCond %{REQUEST_URI} ^/(user|admin|cart) [env=correct_protocol:https]
# Set Current (Actual) Protocol Env Variable:
## Again, default to http
SetEnv current_protocol http
## Overwrite with https if %{HTTPS} = on
RewriteCond %{HTTPS} = on [env=current_protocol:https]
# If current <> correct
RewriteCond %{ENV:current_protocol} != %{ENV:correct_protocol}
# rewrite URL using correct:
RewriteRule .* %{ENV:correct_protocol}://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
I know that there is a lot of examples on Stackoverflow but I still miss something.
I'm trying to redirect http://old.domain.com/fr/ to http://brand.new-domain.com/fr/ with the following rules, but that doesn't work:
# Enable Rewrite Engine
RewriteEngine On
RewriteBase /
# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
# Redirect domain
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [r=301,L]
# Remove index.php
# Uses the "exclude method"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Exclude_List_Method
# This method seems to work best for us, you might also use the include method.
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
# Exclude root files
RewriteCond $1 !^(index\.php) [NC]
# Exclude EE folders
RewriteCond $1 !^(assets|ee-admin|images|templates|themes|fr|nl)/ [NC]
# Exclude user created folders
RewriteCond $1 !^(assets|css|img|js|swf|uploads)/ [NC]
# Exlude favico, robots, ipad icon
RewriteCond $1 !^(favicon\.ico|robots\.txt|pple-touch-icon\.png) [NC]
# Remove index.php
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(URL=.*)$ [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
It correctly redirect when I call the root URL, but not when I call a page. What am I doing wrong?
Thanks in advance!
Pv
When writing mod_rewrite rules, the rules get applied in the order that they appear.
To redirect an old domain to a new domain, you'll want that rule to be first in your .htaccess or httpd.conf file — all other rules should appear after it.
If you only want to redirect a certain directory, the following rule will do so, while allowing the rest of the site to function normally:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Only Matching Directories
RewriteCond %{REQUEST_URI} ^/(fr|fr/.*)$
RewriteRule ^(.*)$ http://brand.new-domain.com/fr/$1 [R=301,L]
</IfModule>
If you want to redirect the entire site, the following rule will do so:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^old.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com$ [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
</IfModule>
If you care about letting crawlers know your content has moved and want to make the transition as seamless as possible, be sure to keep the 301 Redirect flag in the RewriteRule.
This will ensure that users and search engines are directed to the correct page.
While we're on the subject, as part of the EE 2.2 release, EllisLab now "officially" offers limited technical support for removing index.php from ExpressionEngine URLs.
Simply add or update your code to the following, making sure to consider any rules you may already have in place:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
Try to use the following ruke as the first one:
# Redirect domain
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
Also mind the upper case R with is the short form for the lower case redirect.
Have you tried using mod_alias simple redirect instructions (a core module that you have), before trying the hacky-mod-rewrite thing?
I would do a VirtualHost with ServerName old.domain.com and in this VH I would add this rule:
Redirect /fr http://brand.new-domain.com/fr
from doc:
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
So get a separate VirtualHost for brand.new-domain.com (with ServerName brand.new-domain.com) and in this one do not set the Redirect Rule.
If you still want to handle the 2 domains in the same VirtualHost then you'll have to use mod-rewrite as even RedirectMatch cannot check the request domain on the query.