Links on Subdomain broken after htaccess rewrite - .htaccess

Problems:
1) Need ALL urls to always redirect to non-www
2) domain.com needs to automatically redirect to sub.domain.com
Based on other articles here, I've tried:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(sub\.domain\.com)$ [NC]
RewriteRule ^ http:://%1%{REQUEST_URI} [R=301,L]
[had to insert an extra ":" for this to post]
Which "works", but then all of the links on my site 404.
Can you point out what I'm doing wrong?

Try this code :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]

In the comment you provided on Jul. 26th, the rule
RewriteRule ^(.*)\.html$ $1.php [nc]
is used to rewrite all requests for html files to requests for php files. This is typically done for SEO, to make the pages appear as static html instead of dynamic php in situations where php is primarily used to reduce code duplication.
In these cases, it is typical that all the page files on the server exist only in php format, and not html format. Therefore, without this rule, all links to .html files will remain as requests to the server for .html files, which will result in many 404s.
I would suggest you simply insert this rule in your .htaccess block in the original question, at the end of your statements. They ought to play well together. The result for completeness:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(sub\.domain\.com)$
RewriteRule ^http://%1%{REQUEST_URI} [R=301]
RewriteRule ^(.*)\.html$ $1.php [NC]
*My mistake, the flags [NC] and [OR] are definitely valid for RewriteCond statements.

Related

How can I create an exception in .htaccess?

I try to solve folling problem with .htaccess.
I use it to rewrite everything to "https://" and put "www" in front of every url.
Now I want to use a SSL-certificate. To validate it, I put a html-file in a certain folder. I do not want this to be redirected to "www.". How can I create an exception only for this one file?
Thank you very much for helping me with this maybe kind of stupid question.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
You did not provide an example of URL that should not be redirected to https. But here is an example. This is a simplified version of your .htaccess file.
I have combined the https and www redirect in one directive.
Assume your domain name is example.com.
There is a negative condition (note the exclamation mark). If the request URI does not begin with index.html then the rule fails and is not enforced.
So http://example.com/index.html will not be converted to https, but there is one important caveat: in this case the www. will not be added either. I understand this what you want, then we can have a simplified set or rules.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/index.html
RewriteRule (.*) https://www.example.com/$1 [L,R=301]

Redirect 2 directories to a particular URL

I already have this written in my .htaccess file.
Redirects all traffic to my newsite.co.uk except for oldsite.net/dlg and oldsite.net/members
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/dlg(/?$|/.*$) [NC]
RewriteCond %{REQUEST_URI} !^/members(/?$|/.*$) [NC]
RewriteCond %{HTTP_HOST} !^.*newsite.co.uk.*$ [NC]
RewriteRule ^(.*)$ http://www.newsite.co.uk/$1 [R=301,L]
However my e-commerce pages at oldsite.net/dlg and oldsite.net/members are currently down. So until these pages are back up, is it possible to rewrite the above code, to remove the exception but make /dlg and/members divert to a particular page on my newsite.co.uk?
Thanks in advance!
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newsite.co.uk// [R=301,L]
for further information
http://www.webconfs.com/how-to-redirect-a-webpage.php

www htaccess redirect with segment in url

Where my site is hosted, I'm using .htaccess and it has a condition to remove the www and direct to the main page without the www.
<ifModule mod_rewrite.c>
Header set X-Frame-Options DENY
RewriteEngine On
# Required to allow direct-linking of pages so they can be processed by Angular
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://meusite.com.br [R=301,L]
</ifModule>
The problem is this, when someone accesses an internal page with www, it falls for this check and is directed to the home, example:
If someone accesses the link: http://www.meusite.com.br/conteudo/94-vai-criar-um-site-to-your-employee-said-you-can-noble
It will direct to http://meusite.com under the condition.
What I need, is that it is directed to the following link: http://meusite.com/content/94-vai-create-a-site-to-your-employee-behavior-which-cannot-can- -fine only by removing the www from the link.
Does anyone know if this check is possible in .htaccess?
EDIT:
.htaccess is not able to translate your titles from portugese to english.
You should do redirection to normal domain with full link, and then do internal redirection with your backend (i.e. php, ruby) to proper translated link.
Use following code before your redirection, so links with conteudo will be catched here and redirected properly using backreferences:
RewriteCond %{HTTP_HOST} ^www\..* [NC]
RewriteRule ^\/conteudo\/(.*)$ http://menusite.com.br/content/$1 [R=301,L]
Soluction:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Domain based redirect to different PHP files via htaccess

Domain based redirect to different PHP files via htaccess
Hello there.
Here's the thing:
I have two domains here [http://www.myproject.com and
[http://www.myproject.com.br .. In my root folder i have all the stuff for
my projects including two "indexes": "index-en.php" for english and
"index.php" for portuguese.
Can i have some trick in htaccess that redirect my users to one
of these files depending on domain? .. something like:
[http://www.myproject.com [OR]
[http://myproject.com redirect to [http://www.myproject.com/index-en.php
and
[http://www.myproject.com.br [OR] [http://myproject.com.br redirect to
[http://www.myproject.com.br/index.php
??
Sorry if this is a stupid question but im almost crazy looking for tutorials over
the internet and i cant get something that works or some response if this is possible or just a stupid question .. Can someone please give me some direction?
Maybe some solution with a conditional directoryIndex (I dont know if that's possible).
thanks a lot
Actually i have this in my .htaccess:
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
#Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^myproject.com$ [NC]
RewriteRule ^(.*)$ http://www.myproject.com/index-en.php [R=301,L]
RewriteCond %{HTTP_HOST} ^myproject.com.br$ [NC]
RewriteRule ^(.*)$ http://www.myproject.com.br/ [R=301,L]
</IfModule>
This just does the "non www to www" - that works correctly
This should do what you want. The RewriteCond trickery is probably a bit overkill here, but it does make adding extra languages easier:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST}/index.php \.com\.br/(index.php)$ [NC,OR]
RewriteCond %{HTTP_HOST}/index-en.php \.com/(index-en.php)$ [NC]
RewriteRule ^ %1
RewriteCond %{HTTP_HOST} myproject\.com$
RewriteRule ^/$ /index-en.php
RewriteCond %{HTTP_HOST} myproject\.com\.br$
RewriteRule ^/$ /index.php

.htaccess mod_rewrite redirection between domains

I have two domains, cshen.ca and cshen.net, both point to the same place.
I'm trying to accomplish the following with mod_rewrite:
cshen.net is 301 redirected to cshen.ca
www.cshen.net or www.cshen.ca are both redirected to cshen.ca
the path after the domain is preserved after being redirected, for example www.cshen.net/foo/bar/ would be redirected to cshen.ca/foo/bar/
I've tried a variety of rules, but can't seem to get it to work.
RewriteCond %{HTTP_HOST} ^cshen\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://cshen.ca/$1 [R=301,L]
This accomplishes the first two rules, but redirects everything back to the home page, and does not preserve the rest of the URL.
RewriteCond %{HTTP_HOST} ^cshen\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^/(.*)$ http://cshen.ca/$1 [R=301,L]
Making a small change and adding a slash '/' to the RewriteRule preserves the rest of the URL, but only www.cshen.ca is being redirected. Neither cshen.net nor www.cshen.net are being redirected anywhere.
I've also tried Apache's guide and used this code:
RewriteCond %{HTTP_HOST} !^cshen\.ca [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://cshen.ca/$1 [L,R]
I thought this would work, since it should redirect any url that isn't cshen.ca, however, like the second piece of code, it does nothing to cshen.net or www.cshen.net.
I've just about ran out of ideas of other things to try. I would appreciate it if someone could help out!
Addendum: in case it matters, I'm using WordPress's pretty URLs, and the rewrite rules for that are:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried putting my rules before and after the WordPress rules (when I put it before I'd of course add RewriteEngine On before my rules), doesn't make any difference.
Thanks!
don't know how to edit, solved by placing the code from apache before WP's
must have forgotten to test this earlier.

Resources