I need to redirect users accessing mysite.com to www.mysite.com in my .htaccess file.
The problem is anything I try won't work, I think because ^mysite.com doesn't exclude www.mysite.com so that will create a bad loop, but I usually get error 500.
I also need to exclude another subdomain, the img.mysite.com.
How can I do that?
I used many examples from the net & a few htaccess generators, which gave me this code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite.com[nc]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [r=301,nc]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is what you need - taken from the default Drupal .htaccess
Try something like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mywebsite.com$ [NC]
RewriteRule ^/(.)$ http://www.mywebsite.com/$1 [L,R=301]
Related
I want to redirect the the subdomain to subdirectory but not working. Here is my efforts.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^name\.site_url
RewriteRule ^(.*)$ http://site_url/name/$1 [L,R=301]
Try this:
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(.*)$ http://your_domain/sub/$1 [R,L]
Instead sub in RewriteCond and RewriteRule you can place whatever you want
Btw, RewriteCond %{HTTP_HOST} ^sub\.my\.domain$ also works for me. So, check your site url. Or give more info (at least, what happened in apache error.log).
I'm trying to redirect foo.bar.com to bar.com/foo.php, but I have no idea where to start and everything else has failed me so far. I've tried many things here on StackOverflow, but none of them seem to be working at all (just various Apache errors).
I already have a wildcard subdomain set up from cPanel, but I'm not really sure where to go from here. Is .htaccess even the best option for this?
EDIT: I also want to rewrite the URL from bar.com/foo.php to foo.bar.com.
You can use this generic rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(.+)$ [NC]
RewriteRule ^/?$ http://%2/%1.php [L,R=302]
EDIT: To hide the rewritten URL (internal rewrite) use this:
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(.+)$ [NC]
RewriteRule ^/?$ %1.php [L]
My website is hosted on a VPS powered by Ubuntu with LAMP.
I can't use direct links to any page without www.
that is typing url http://example.com/secondpage will redirect to http://www.example.com/.
the second page had lost. But http://www.example.com/secondpage works perfectly.
I tried changing .htaccess, but no effect.
Can you help me?
You need to use this inside your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
If this doesn't work, you can use this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
Good evening,
I wanted to know if it is possible to redirect a request to my page as http://test.diogo.me to the page with the content -> http://www.diogo.me/pages/test/ (this redirection must be internal... the browser should point to http://test.diogo.me).
I have been reading about .htaccess and I feel that this is the resolution for me.
I have tried with the following code:
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} !^www\.diogo\.me [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.diogo\.me [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
The output is error 404 file not found :(Am I going in the right track? Sorry about my expressing limitations. Thanks in advance
Just put this code in your .htaccess and remove your existing lines from there:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.diogo\.me$ [NC]
RewriteRule ^$ http://www.diogo.me/pages/test/ [L,P]
You were missing the /pages part
RewriteCond %{HTTP_HOST} !^www\.diogo\.me [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.diogo\.me [NC]
RewriteRule ^(.*)$ /pages/%1/$1 [L]
Assuming the subdomains have the same document-root as the main domain.
I'm not too familiar with .htaccess files, and I'm trying to exclude a subdomain (something like dev.example.com) from the following rewrite rule that's already in place:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
This rule prohibits anyone from just entering example.com or http://example.com and forces the desired presentation of the URL, http://www.example.com.
I've tried a few options of excluding a subdomain from this rewrite rule, but to no avail. Each of the directories on the site have their own .htaccess file, but it seems this one is still taking precedence. Any ideas? Thanks!
The existing rule already excludes a subdomain. You just need to append a new condition:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
You can also get rid of regular expressions:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com [NC]
RewriteCond %{HTTP_HOST} !=dev.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
The syntax can be found at http://httpd.apache.org/docs/2.2/en/mod/mod_rewrite.html#rewritecond
I'm not sure if this could fit for you, but it is good practice to use apache virtualhosts configuration instead of .htaccess files. In that case, for the non-www -> www redirect, I usually use something like:
<VirtualHost *:80>
ServerName example.com
RedirectMatch permanent ^(.*) http://www.example.com$1
</VirtualHost>
That is generally safer than a mod_rewrite rule.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(dev|www)\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]