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]
Related
I want to redirect both
https://website.me/ and https://www.website.me/ to https://es.website.me/
This rule doesn't work
RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://es.website.me/$1 [R,L]
Use below htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?website\.me$ [NC]
RewriteRule ^(.*)$ https://es.website.me/$1 [R=301,L]
Try this one for redirection in your case:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^website.me$ [OR]
RewriteCond %{HTTP_HOST} ^www.website.me$
RewriteRule (.*)$ https://es.website.me/$1 [R=301,L]
</IfModule>
Do you want to redirect, or rewrite?
To redirect with a code 301 (permanently moved), make 2 virtual hosts; one for the real site, and one for all the URL's you want to redirect. In the redirect host, add this line:
Redirect 301 / https://es.website.me/
Since you want to redirect only if HTTPS is already used, you must check for it, together with the hostname of course.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?website\.me$ [NC]
RewriteRule ^ https://es.website.me%{REQUEST_URI} [R,L]
When everything works as it should, you may replace R with R=301. Never test with R=301.
I have the following .htaccess-File:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
This should change the prefix of the input always to http://www.example.com/something but this doesn't work and I don't know why not. It redirects only to www.example.com...
I have tried a lot but I couldn't find a solution. Could it be, that the browser doesn't display http://?
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]
I have make virtual subdomain in my code.like below
RewriteCond %{HTTP_HOST} ^(.*)\.mysitename\.com
RewriteRule ^(.*)$ agent.php?asitename=%1 [L,NC,QSA]
it works fine, but it did not work for pages like
RewriteCond %{HTTP_HOST} ^(.*)\.mysitename\.com
RewriteRule ^(.*)/ag_buy.html ag_buy.php?sitename=%1&page=buy [L,NC,QSA]
it redirect all pages top agent.php, but it should only redirect home page to agent.php, for other pages it should work like ag_buy.html to ag_buy.php
and so on.........
please guide me on htaccess how can i make this possible.
The ^(.*)$ matches everything, if you only want the home page, then change it to ^$:
RewriteCond %{HTTP_HOST} ^(.*)\.mysitename\.com
RewriteRule ^$ agent.php?asitename=%1 [L,NC,QSA]
Try to replace the first rule with following code:
RewriteCond %{HTTP_HOST} ^(.).mysitename.com
RewriteCond %{REQUEST_URI} =/
RewriteRule ^(.)$ agent.php?asitename=%1 [L,NC,QSA]
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]