htaccess - multiple domain to subfolders - php

i'm try to do this thing:
I want to redirect all the traffic for a specific domain A to a subfolder of the root. I will do the same thing with multiple domains with their subfolder.
Example:
www.domaina.it -> /public_html/domainadir/
www.unioncucine.it -> /public_html/unioncucine/
For that i have done it using this code that seems to work:
DirectoryIndex index.php index.html
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/unioncucine/
RewriteCond %{HTTP_HOST} ^(www\.)?unioncucine\.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /unioncucine/$1
RewriteCond %{HTTP_HOST} ^(www.)?unioncucine.it$
RewriteRule ^(/)?$ /unioncucine/index.html [L]
It seems to work but not in this case:
I want to access www.unioncucine.it/web in which i have loaded a test joomla page (it has not template because i'm working on it).
If you go here: www.unioncucine.it/web i'm redirected to www.unioncucine.it/unioncucine/web/ and it does not load properly contents.
Going here: www.unioncucine.it/web/ it works fine. (The page is just some words and a css scretch).
#Changing
RewriteRule ^(.*)$ /unioncucine/$1
#To
RewriteRule ^(.*)$ /unioncucine/$1/
It seems to work without redirect but all the files inside other folders are not available.
Thank you
Edited:
Changed as you told:
RewriteCond %{REQUEST_URI} !^/web/templates/protostar/ [NC]
RewriteRule ((?:css|js|images)/.+)$ /web/templates/protostar/$1 [L,NC,R=301]
RewriteCond %{REQUEST_URI} !^/unioncucine/
RewriteCond %{HTTP_HOST} ^(www\.)?unioncucine\.
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /unioncucine/$1/
RewriteCond %{HTTP_HOST} ^(www.)?unioncucine.it$
RewriteRule ^(/)?$ /unioncucine/index.html [L]
But it still don't work: http://www.unioncucine.it/css/template.css

You're getting redirected from www.domain.it/domainadir/test to www.domain.it/domainadir/test/ due to mod_dir which adds a trailing slash after a directory.
Add these 2 lines on top of your .htaccess before other existing code:
DirectoryIndex index.html
DirectorySlash Off
Though you should add rule to add trailing slash as well in your rule as:
RewriteEngine On
# fix css/js/images
RewriteCond %{REQUEST_URI} !^/web/templates/protostar/ [NC]
RewriteRule ((?:css|js|images)/.+)$ /web/templates/protostar/$1 [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/domainadir/
RewriteCond %{HTTP_HOST} ^(www\.)?domaina\.
RewriteRule ^(.*)$ /domainadir/$1/ [L]
For css/js/images better to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can try adding this in your page's header:
<base href="/" />

Related

htaccess rewrite to subdmains not working

I am trying to rewrite a subdomain sub.example.com to example.com/works/sub. I have the following in my htaccess file, but its not working. Please help.
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^/(.*)$ http://example.com/%1/$1 [L,NC,P]
If have also tried the following:
RewriteCond %{HTTP_HOST} ^sub\.example\.com
RewriteRule ^(.*)$ http://example.com/works/sub/$1 [L,NC,QSA]
In both cases, I get ERR_NAME_NOT_RESOLVED whenever I try to navigate to the page.
The funny thing is that every other rule in the htaccess file works. Including the rule that follows it immediately:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Just that first part is not working.

how to redirect all page request to one page without url change

I want to redirect all pages on my site (including index) to UnderWork.html
and I'm doing this using .htaccess with this code:
RewriteEngine on
RewriteRule ^(.*)$ UnderWork.html
...and its works fine.
Now I am adding some more code to my .htaccess to redirect all traffic to the non-www domain and now my code looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.in
RewriteRule ^(.*)$ http://domain.in$1 [R=permanent,L]
RewriteRule ^(.*)$ UnderWork.html
Here is where I have a problem. If I enter a URL like: domain.in/xyz then it works as before, but if I use a URL like: www.domain.in/xyz then Apache converts it to coincart.inxyz/.
What am I doing wrong here? How can I get what i want? Thanks in advance.
Below are rules that work for me:
RewriteEngine On
# Adding trailing slash for directory requests.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/www$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.+[^/])$ http://example.com/$1/ [R=permanent]
# External redirection from www subdomain to non-www domain.
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/?(.*) http://example.com/$1 [L,R=permanent]
# Internal redirection to index.php for nonexistent URLs.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|png|gif)$
RewriteRule . /index.php [L,QSA]
To show a custom page when a directory root (domain root in particular) is requested, use the Apache's DirectoryIndex directive:
DirectoryIndex UnderWork.html

Remove URL file extension but retain file structure?

I have a URL like http://example.com/dev/testSite/ where the root of the testSite is in the dev directory. I have that directory set up like:
testSite
-images/
--image.png
-css/
--style.css
header.php
footer.php
index.php
page.php
I have also included in my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
The first issue with this is instead of going to page.php like http://example.com/dev/testSite/page/ it is instead redirecting to http://example.com/page/. Eventually this site will live at http://testSite.com so I assume this code is going to work correctly. In the meantime, is there any fast I can do to make it work in the dev environment? Something that I can change quickly when moving it to the production environment.
The second issue is that this is also altering the file structure of the files. For example, any code on page.php like <img src="images/image.png"> is actually trying to find this image in http://example.com/dev/testSite/page/images/image.png. I suppose this .htaccess is making it behave as if I added the directory "page" instead of just hiding the extension.
There is no kind of framework or anything on the site so just regular php pages.
Your .htaccess should be like this in /dev/ sub-directory:
RewriteEngine On
RewriteBase /dev/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ $1/ [R=301,L]
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php [L]
Then to fix relative paths in css/js you can add this in the <head> section of your page's HTML:
<base href="/dev/testSite/" />

301 redirect URLs that are also being rewritten

I am building a site using Slim, which suggests the following .htaccess code for creating pretty URLs:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
This means that a URL like http://example.com/account/login will alias for http://example.com/index.php/account/login, which is then processed by my front controller. So far so good.
However, if someone explicitly navigates to
http://example.com/index.php/account/login,
I would like it to first redirect to:
http://example.com/account/login,
and then alias for
http://example.com/index.php/account/login.
That way, the user will see http://example.com/account/login in their navigation bar.
How can I handle both of these rules simultaneously in .htaccess, and most importantly, without hardcoding the host and domain (http://example.com) in .htaccess?
This should also work in a subdirectory, for example:
http://example.com/site1/index.php/account/login ->
http://example.com/site1/account/login
With the .htaccess file located in the site1 directory relative to document root, and without having to explicitly put site1 in the .htaccess.
You need a new redirect rule before this rule:
RewriteEngine On
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ %{ENV:BASE}$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Try this:
RewriteRule ^index\.php/(.*)$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

.htaccess - Redirect subfolder to index.php using subfolder name as variable

This is driving me mad. I'm trying to use .htaccess to redirect a subfolder (that doesn't exist) to the index page, using the subfolder name as the variable. ie:
http://www.website.com/john/
redirects to:
http://www.website.com/index.php?name=john
I've tried this (and various others) with no luck:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?name=$1
Here is an example, how you can do this:
# turn mod_rewrite engine on
RewriteEngine On
# rewrite a physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/layout/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"
# rewrite rules
RewriteRule .* - [L]
RewriteRule (.*) index.php?_route=$1 [QSA]
This one also denies access to folders you don't want to have public.
Try this one:
RewriteRule ^([^/]*)/$ /?name=$1 [L]
RewriteRule ^([^.]*)$ /index.php?name=$1 [L,QSA]

Categories