I am working on a project in PHP and using Apache server.
Say, my project's url is myurl.com.
When i visit
http://myurl.com
it works well.
But when I visit
http://www.myurl.com OR www.myurl.com
it redirects me to http://myurl.com, dropping the www subdirectory.
I want it to stay same, like if we visit http://www.myurl.com it should keep the "www" in url.
I believe this is a .htaccess setting.
I couldn't get the settings right.
Please guide
Thanks
You should open the htaccess file and search for something like
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.myurl\.com$ [NC]
RewriteRule ^(.*)$ http://www.myurl.com/$1 [R=301,L]
This code is redirecting.
Look further here: http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html#m0-askapache2
Related
I have edited my /etc/hosts file to link domain.com to my localhost. Now when I visited domain.com I want it to get the site contents from domain.com/site/ but only using the URL domain.com thus removing site/ from the URL.
I have done this once before, so I know it's possible, but I lost the code. Can someone please assist me? .htaccess scripting is not my strong. Apologies if this is a duplicate of something else.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^((?!site/).*)$ site/$1 [L,NC]
I have managed to get this working with the following code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)your_directory
RewriteRule ^(.*)$ your_directory/$1 [L]
I have a website. I created a mirror of it and uploaded it in a directory called 'mirror'. What I wanted to do is whenever a viewer access for example..
http://www.example.com/this-page/another-segment/?id=1
I want him to be redirected to..
http://www.example.com/mirror/this-page/another-segment/?id=1
^^^^^^
(I am doing this because I want to want to edit my site's design but I don't want viewers to see the changes in progress until they are complete. Thus I want to redirect them to the mirrored snapshot, at least temporarily.)
Please suggest how this can be done using .htaccess or not.
after browsing around the internet. i came up with the idea of putting a /$1, a rewritebase and followsymlinks on the answer givien by appclay
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/mirror/
RewriteRule (.*) /mirror/$1 [R=301,L]
You'll want to do something like:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/mirror/
RewriteRule ^/(.*) /mirror/$1 [R]
In your .htaccess file.
I've searched everywhere and used some examples but they don't meet my specific needs, hence why I'm asking here if anyone can please help? I know the http:: below isn't correct, it's just because I can't post links here.
I'd like to redirect http:://www.mysite.co.uk/ to http:://mysite.co.uk/
whilst also being able to redirect
http:://www.mysite.co.uk/mysub/
and 2. http:://mysite.co.uk/mysub/
to 3. http:://mysub.mysite.co.uk/
All files in 1. and 2. should be redirected to their equivelant in 3.
For example: http:://www.mysite.co.uk/mysub/file.html AND http:://mysite.co.uk/mysub/file.html should both go to http:://mysub.mysite.co.uk/file.html
This is to affect the subdomain only, so that other folders and files in the site root aren't affected.
If anyone could help me understand and write the code for the 301 redirect in a .htaccess file I'd really really appreciate it! Thanks!
This is the code you'll need in your .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# To redirect http:://www.mysite.co.uk/ to http:://mysite.co.uk/
RewriteCond %{HTTP_HOST} ^www\.(mysite\.co\.uk)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# To redirect /mysub/foo to http://mysub.mysite.co.uk/foo
RewriteCond %{HTTP_HOST} ^(www\.)?(mysite\.co\.uk)$ [NC]
RewriteRule ^(mysub)/?(.*)$ http://$1.%2/$2 [R=301,L,NC]
I have hosting setup with a master domain (mapped to the web root) and then a number of addon domains (each with their own folder within the web root). At the moment you can visit www.masterdomain.com/addondomainsubdir and reach the same page as you would if you visited www.addondomain.com (which maps to /public_html/addondomainsubdir). I want to prevent this so if you visit www.masterdomain.com/addondomainsubdir then it will do a 301 redirect to www.addondomain.com. The new addondomain.com site is a single page site so it does not have to map any additional pages.
Adding rules to the htaccess file in the web root does notaffect anything as the subdir exists which is wierd as i thought the htaccess command should work even if there is a matching subdir (i've tried the following which works when there's no matching subdir):
RewriteRule ^addondomainsubdir?$ http://www.addondomain.com [NC,R=301,L]
Logically given it's reaching this directory I figure i need to add a command within the htaccess file in the addondomainsubdir directory however nothing appears to have any effect (i've got various other rules setup and they work fine).
I would be massively grateful if anyone explain the best way to rectify this?
Thanks so much for your help,
Dave
I know this is an old post, but it has never been successfully answered. So for all of you finding this via search, this should do what the OP is asking.
Add this line to your .htaccess file:
redirect permanent /addondomainsubdir/ http://www.addondomain.com
Try these rules in your .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for http
RewriteCond %{HTTP_HOST} ^(www\.)?masterdomain\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^([^/]+)/?$ http://www.$1.com/ [R=301,L]
# for https
RewriteCond %{HTTP_HOST} ^(www\.)?masterdomain\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^([^/]+)/?$ https://www.$1.com/ [R=301,L]
Instead of putting a rule in your main .htaccess, I would make make a .htaccess for each add-on domain, putting each one in the respective subdirectory.
RewriteEngine on
RewriteCond %{HTTP_HOST} masterdomain\.com$ [NC]
RewriteRule ^addondomainsubdir(.*)$ http://www.addondomain.com/$1 [R=301,L]
I've just setup a subdomain for my site.
My problem is:
when I go to http://xyz.site.com I didn't see anything. Only "The link appears to be broken".
But when I go to http://xyz.site.com/index.html all it works fine.
The problem is in .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^([a-z0-9]+)/?$ profile.php?id=$1 [NC]
If I comment the RewriteRule it works correctly.
How can I fix the htaccess? Maybe with a RewriteCond?
Thanks.
I will set a default page in .htaccess if I were you.
DirectoryIndex index.html