www htaccess redirect with segment in url - .htaccess

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]

Related

Under construction page dosen't load - 302 redirect from htacces

I want to redirect all my traffic to a "under construction" ( coming soon) page but the style of page dosen't load. I think I need to redirect all traffic to /under-construction/ folder but if I try that I receive a loop redirect error ( too many redirects )
I use this rewritre code using .htaccess to redirect all traffic except my ip.
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^5.xx.xxx.xx
RewriteCond %{REQUEST_URI} !/under-construction/index\.html$
RewriteRule .* http://gopal.net.cn/under-construction/index.html [R=302,L]
What I need to do to work?
That was the solution:
# MAINTENANCE-PAGE REDIRECT
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^5.x.xxx.xxx
RewriteCond %{REQUEST_URI} !/under-construction/index.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|php|js) [NC]
RewriteRule .* /under-construction/index.html [R=302,L]
</IfModule>
This code line was needed RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|php|js) [NC] . Without it the js, php, css dosen't load.
May you just want to redirect only the HTML pages, but not the images and css. Then use this:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^5.xx.xxx.xx
RewriteCond %{REQUEST_URI} !/under-construction/index\.html$
RewriteRule (.*)\.html$ http://gopal.net.cn/under-construction/index.html [R=302,L]

Mod rewrite redirect one file to a file on another domain

I have been at this for a number of hours and cannot get this to work. I have other redirects in this .htaccess file that do work.
I need to redirect as follows.
mydomain.com/dir/subdir/myfile.php to myotherdomain.com/dir/my_index.php
Note that mydomain.com and myotherdomain.com both point to the same root directory.
Here's my code.
RewriteCond %{HTTP_HOST} ^(www.)?postle.com$
RewriteCond %{REQUEST_URI} !^/pi_www/hardface/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pi_www/hardface/$1
RewriteCond %{HTTP_HOST} ^(www.)?hardfacetechnologies.com$
RewriteRule ^(/)?$ postle_hft/index_hft.php [L]
I have tired all the permutation of this I can think of and nothing works. I really would appreciate some help.
You can use this rule in /dir/subdir/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^myfile\.php$ http://myotherdomain.com/dir/my_index.php [R=302,NC,L]
If you want to avoid external redirect then:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^myfile\.php$ /dir/my_index.php [NC,L]
should also work as both domains point to same site root.
In the .htaccess file in your document root, try:
RewriteCond %{HTTP_HOST} =mydomain.com
RewriteRule ^dir/subdir/myfile\.php$ http://myotherdomain.com/dir/my_index.php [R=302,L]
The order of directives is important. This should come after your canonical www redirect, but before any internal rewrites, and before any redirects that might conflict.
This also assumes that your canonical URL is the bare domain (ie. not the www subdomain) - like with the example in your question. To match the bare domain or the www subdomain (and make it case-insensitive) then change the RewriteCond directive to:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com$ [NC]
Change the temporary (302) redirect to permanent (301) when you are sure it's working OK. (301 redirects are cached by the browser, so makes testing troublesome.)

How to implement dynamic subdomains in codeigniter with .htaccess?

How can I implement dynamic subdomains in codeigniter with .htaccess?
Make sure that subdomains are enabled on your site. When you enter test.yoursite.com it should take you to the welcome page of your site. If instead it gives DNS lookup error then it means subdomains is not enabled on your site.
To enable subdomains on your site add *.yoursite.com entry to the DNS Zone records.
Second insert the following code in your .htaccess file and replace yoursite appropriately.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#codeigniter specific rule
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#codeigniter specific rule
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#this rule removes www from the URL if its used
RewriteCond %{HTTP_HOST} ^www.
RewriteRule ^(.*)$ http://yoursite.com/$1 [R=301,L]
#the auth system URIs which don't have subdomains
RewriteCond %{HTTP_HOST} ^yoursite.
RewriteRule ^(signup|signin|forgot_password)/?$ index.php?/auth/$1 [L]
#this rule handles the subdomains
RewriteCond %{HTTP_HOST} ^([a-z0-9]+).yoursite.com
RewriteRule ^(.*)$ index.php?/public_site/%1/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Add more rules, if necessary, for handling auth system or subdomains. I know I did for my site to get pretty URIs.
Important Note:
I found that by default the subdomain URIs work very well with codeigniter without any of the above rules. You can access all of your site with test.yoursite.com/# URI instead of www.yoursite.com/#. But the URIs are not pretty and there would be more than one way to access a URI.
So if you use the above rules it will give you pretty URIs and, more importantly, will give you unique URIs. So if you use the above rules you will only get one URI for the signup page and that is http://yoursite.com/signup.
this code is working for my site, you can move your site into your other domain or folder ( whatever you want ).
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|application/assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>
This rule handles the subdomains
RewriteCond %{HTTP_HOST} ^([a-z0-9]+).yoursite.com. should probably also include the hypen -. RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).yoursite.com.

hiding index.php and all the variables from url

I'am using a mod_rewrite to get the virtual subdomain as a variable in my site
and would like to add a rule to hide the index.php and all the variables in the url
but can't get it to work. Any suggestions please?
This is my actual .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com$
RewriteRule ^$ /index.php?member=%1 [P,L]
Your best bet is probably to send in a support ticket and have the domain wildcarded.
.htaccess seems perfect.
Detail Description:
You will need to ask hosting company to add "wild-card" DNS set up to forward all subdomains to Web root directory. For example *.domain.com to www.domain.com. They can do this by adding A entry in DNS setting
HTACCESS Code
<IfModule mod_rewrite.c>
# Redirect to user blog (with any trailing path)
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]).example.com(.*)$ [NC]
RewriteRule ^(.*)$ /index.php?member=$1 [R=301,L]
## Otherwise, force www;
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
</IfModule>

.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