I have several domain names that point to the same IP address. I currently redirect them from the /default.asp file to the appropriate folder based on the SERVER_NAME. For example:
http://domain1.com redirects to http://domain1.com/folder1
http://domain2.com redirects to http://domain1.com/folder2
This works, but the urls display the folder name in the browser:
http://domain1.com/folder1/somepage.htm
And the user can't go to a page without entering the folder name:
http://domain1.com/somepage.htm // This fails because it isn't in the root folder.
Can I use the .htaccess file to (1) route page requests to the appropriate folder and (2) prevent the folder name from appearing in the browser?
That'd also move the redirection from the /default.asp file to the .htaccess file where it probably belongs.
Thanks!
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond %{lowercase:%{HTTP_HOST}} ^www\.([^.]+)\.example\.com$
RewriteRule ^(.*) /home/%1/www$1
Related
I have two version of my site. one is a mobile version and another one is desktop.
Path looks like this :
For Mobile : public_htm/mobile
For Desktop : public_html
I want to change the document root of my domain based on the device. I want to serve content from the mobile folder if the user's device is mobile & serve contents from public_html if user on desktop.
How can I do this with .htaccess?
I want to have my domain the same for both desktop and mobile, only document root changed by .htaccess based on the device.
You would need to add something like the following to the top of the root .htaccess file:
RewriteEngine On
# Prevent direct access to the "/mobile" subdirectory
# - redirect back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^mobile/(.*) /$1 [R=301,L]
# Rewrite all requests from mobile devices to the "/mobile" site
RewriteCond %{HTTP_USER_AGENT} ^iphone|android|etc$ [NC]
RewriteRule ^(?!mobile/).* mobile/$0 [L]
The mobile detection regex (RewriteCond directive) is just an example, the specifics are left up to the reader. (For reference: mobile browser user-agent strings)
The negative lookahead in the RewriteRule pattern prevents requests that have already been rewritten to the /mobile subdirectory from being repeatedly rewritten. If you have another .htaccess in the /mobile subdirectory containing mod-rewrite directives then this may be unnecessary.
The first rule redirects any direct traffic to the /mobile subdirectory back to the document root. However, if you have another .htaccess file in the /mobile subdirectory containing mod-rewrite directives then this redirect would need to be moved to that .htaccess file (it won't work here).
Just a note on terminology... This doesn't strictly change the "document root", as mentioned in the question. (Although it might "look like" it does from a client perspective.) This internally rewrites requests to a subdirectory (URL-rewriting). Server-side applications running in the /mobile subdirectory still see the document root as being the parent directory. You cannot change the "document root" in .htaccess, which can only be done by setting the DocumentRoot (or VirtualDocumentRoot) directive(s) in the server config.
I am attempting to redirect a subfolder on an apache2 server to the root of an entirely different domain, without including the path of the folder in the new domain.
So, in mysite.com/folder-1/folder-2 I have a .htaccess file like so:
RedirectMatch 301 / http://a-totally-different-site.com/
This sort of works, but includes the path in the redirect. So, when you visit mysite.com/folder-1/folder-2 it goes to a-totally-different-site.com/folder-1/folder-2
What I wish to happen, is for the subfolder to redirect to the root, so visiting mysite.com/folder-1/folder-2 will go to a-totally-different-site.com
Is this possible with .htaccess? I have tried a few different approaches I found, but none are working.
Simple rewrite rule should do:
RewriteEngine On
RewriteRule ^ http://a-totally-different-site.com [R=301]
I want to redirect a path: /folder/filename.html to: /folder/newfolder.
My host says I must create a dummy file at /folder/filename.html or it won't redirect. I can't do this as by creating the /folder, that messes up Joomla which would normally create that part of the path dynamically. I'm not sure I believe my host, surely it's possible to redirect a path regardless whether there is a file there?
In your .htaccess after RewriteEngine On just add this line
RewriteRule ^/folder/filename.html /folder/newfolder/filename.html [R=301,L]
I am trying to set up an .HTACCESS so I can have multiple domains with the same root directory. I want the htaccess to redirect to a php file dependant on domain name. I don't want to have to set it up per domain - I want it to be for any domain that I set up with the common root directory to redirect to its own php of the same name. eg
mydom1.co.uk will direct to mydom1.php ,
another.co.uk will direct to another.php
thanks in advance for any help
You can put the following in your .htaccess file at the document root:
RewriteEngine On
RewriteBase /
RewriteRule .* %{HTTP_HOST}.php [L,QSA]
However, I expect you'll need to amend the main rule to handle the request of different pages. At the moment everything will be rewritten to /domain.php
I want to redirect all files (whether it exists or not) in /user directory on my site to a file named temp.php in root directory via .htaccess.
For example, if a user enter user/send.php or user/ or user/send (It may be that not Exists at all), all redirect to temp.php.
How can i do this ?
Try this:
RewriteEngine On
RewriteRule ^user/(.*)$ http://domain.com/temp.php [R,L]