I need to re-direct a url of a website of mine, if it hasn't the www in it, because of a script that dont work if the url isn't complete, how can i do this with a htacces file, I cant find an example on the web.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
Something like this might work for you:
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http://www.%1%2 [R=301,L]
Related
Trying to get
www.example.com
to go directly to
www.example.com/forum
How can I do this with this configuration? Thanks in advance.
Edit:
Right now I have added below content in .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteRule ^(.*)$ /forum/$1 [L,R=301]
I want result like
https://www.example.com/topic/topic-url/
to
https://www.example.com/forum/topic/topic-url/
With your shown samples, could you please try following. Please clear your browser cache before testing your URLs. Your tried rules will be creating an infinite loop hence it may not be working, I have added a condition if uri doesn't start from forum then only proceed with redirection.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_URI} !^/forum [NC]
RewriteRule ^(.*)$ /forum/$1 [L,R=301]
I have the following set up for an old domain, which works really well to mapping the old pages to the ones on the new domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldurl.org.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.org.uk [NC]
RewriteRule ^(.*)$ http://www.newurl.co.uk/$1 [L,R=301,NC]
The problem is I need to redirect the homepage to a specific page on the new URL, keeping the above in tact. Whats the best way to do this?
Thanks in advance!!
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org\.uk$ [NC]
RewriteRule ^$ http://www.newurl.co.uk/specificPage [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org\.uk$ [NC]
RewriteRule ^(.+)$ http://www.newurl.co.uk/$1 [L,R=301]
I have searched the web all over for a good generic .htaccess script for redirecting non-www to www, and currently i'm using this:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
This works fine, but if i go to a subdomain www. will be added. Does anyone has a good working redirect .htaccess script?
Try this :
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.([^\.]+)\.([a-z]{2,4})$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
#Vince What if the requested url is something like:
http://www.abc.example.com
IMHO, I think with your method it would never be redirected to:
http://www.example.com
How about this?
# Rewrite domain
RewriteCond %{HTTP_HOST} !^www\.([a-z1-9\-]+)\.([a-z]+)$ [NC] [and]
RewriteCond %{HTTP_HOST} ([a-z1-9\-]+)\.([a-z]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
Also, you guys may find these references useful:
https://www.drupal.org/node/93603
http://www.askapache.com/htaccess/modrewrite-tips-tricks.html
I'm trying to do something like this...
Redirect www.mysite.com/directory/* to my subdommain.mysite.com/directory/*
Please help me
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^directory/(.*)$ http://subdommain.mysite.com/directory/$1 [R=301,L]
I want like it
Try adding this to your .htaccess file if you want to redirect all requests from www.mysite.com to subdomain.mysite.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://subdomain.mysite.com/$1 [R=301,L]
Try adding this to your .htaccess file if you want to redirect only requests for /anydirectory/ from www.mysite.com/anydirectory/ to subdomain.mysite.com/anyotherdirectory/:
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^anydirectory/(.*)$ http://subdommain.mysite.com/anyotherdirectory/$1 [R=301,L]
I'm trying to implement a solution using .htaccess and wildcard subdomains so that
http://subdomain.example.com is mapped to http://example.com/index.php/accounts/subdomain/. My rules look something like:
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*/) /index.php [PT,L]
Which works, but disregards everything else. When I try appending anything to the rule e.g:
RewriteRule ^(.*/) /index.php/hello [PT,L]
I get a 500 internal server error. How do I get this working?
You probably need to exclude the index.php from your rule:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]
This is an adaptation of the code I use to redirect subdomains on my own site. I make no claims to it being best practice but it works;
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.example$ [NC]
RewriteRule ^.*$ http://www.example.com/index.php/accounts/%1/ [R=301,L]
Try changing your RewriteRule to
RewriteRule ^/(.*)$ /index.php/accounts/%1/$1 [PT]
That will rewrite the URL to one that includes the subdomain and the original request URI.
EDIT: maybe it needs to be
RewriteRule ^(.*)$ /index.php/accounts/%1/$1 [PT]
as mentioned in the comments.