.htaccess redirection to directories depends on URL - apache

1) I have such directory structure:
.htaccess
album/
index.php
web/
.htaccess
index.php
...
...
In 'album' there is a gallery page and in 'web' there is a WordPress page.
How to fill .htaccess (I suppose in root dir) to redirect addresses depends on what is in URL:
www.domain.org -> goto /web and display wordpress page
www.domain.org/album -> goto /album and display gallery page
I've tried such thing in root .htaccess but without success (there is constant redirection to domain.org/web):
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^(.*)$ /web
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org/album$ [NC]
ReWriteRule ^(.*)$ /album [L]
.htaccess in web directory is default of WordPress:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /web
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
How to solve this riddle?
Regards.
------------------
One of possible answer to thequestion (
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^album(.*)$ album$1 [L]
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule web/$1 [L]
Weard is last line of this code. There are two spaces. I suppose first space means "empty string". This is strange (and also bad coding) however it works and I have enough of those redirections.

You can use these rules in root .htaccess:
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^((?!album).*)$ web/$1 [L,NC]

Related

Forcing HTTPS & NON-WWW on a subfolder with a different domain rewrites incorrectly

I already have a Wordpress website installed to /home/USER/public_html/ and the .htaccess inside the folder looks like:
# BEGIN Redirect Alias Site to Specific Folder
RewriteCond %{HTTP_HOST} customwebsite.com$ [NC]
RewriteRule ^(.*)$ /customwebsite/$1 [L]
# END Redirect Alias Site to Specific Folder
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The redirect alias rules in the .htaccess file above allows me to route my alias domain to use a different folder thus allowing mulitple websites to be ran from the same cPanel account.
In the folder /home/USER/public_html/customdomain/ I've set the .htaccess rules to:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /customdomain/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /customdomain/index.php [L]
</IfModule>
# END WordPress
It works perfectly fine; however if I try and FORCE HTTPS and NON-WWW using every set of rules I can find on the internet... for example:
<IfModule mod_rewrite.c>
# Force HTTPS & NON-WWW
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^customdomain\.com$ [NC]
RewriteRule ^ https://customdomain.com%{REQUEST_URI} [R=301,L]
</IfModule>
and what I can make myself and then try and load any of the following:
http://customdomain.com
https://www.customdomain.com
http://www.customdomain.com
They all redirect to:
https://customdomain.com/customdomain/
Which is partly correct, but partly wrong. It shouldn't have the folder at the end of the domain which causes problems because the URL is now incorrect.
Sure if I remove the %{REQUEST_URI} it fixes the redirect issue but if I am using Wordpress for example every URL will be wrong and will throw up a 404 page not found.
How can I fix the Force HTTPS & NON-WWW to allow for this scenario?
You can use this rule as your first rule in /customdomain/.htaccess:
RewriteEngine On
RewriteBase /customdomain/
# Force HTTPS & NON-WWW
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^customdomain\.com$ [NC]
RewriteRule .* https://customdomain.com/$0 [R=301,L,NE]
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Make sure to clear your browser cache or use a new browser for testing.

how to forward url containing certain directory to same url on new subdomain

Just launched a new site. Kept the old site, but assigned it to a subdomain.
I need to forward requests for old site URLs to the new subdomain.
Old site URLs all have this structure: "www.oldsite.com/BMT/xxxxxx" where the "/BMT/" directly follows the .com.
I need my .htaccess to forward URLs like that to "subdomain.oldsite.com" instead of "www.oldsite.com", keeping the same path after the "/BMT/"
Any URLs without the "/BMT/" should not be redirected.
From my research, I think this might work
RewriteCond %{REQUEST_URI} ^/BMT http://subdomain.oldsite.com%{REQUEST_URI} [L,R=301]
Am I on the right track?
If so, where should I add it in my current .htaccess?
Current .htaccess is:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Yes, you are right,
RewriteCond %{REQUEST_URI} ^/BMT [NC]
is a condition ,if it matchs the request uri string starting with /BMT then the rule will redirect the uri to http://sub.domain .
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} ^/BMT [NC]
RewriteRule ^(.*)$ http://sub.oldsite.com/$1 [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

htaccess subdirectory + 301 on www

I'd like to do 2 things with htaccess but I can't figure out how to do it.
Let's say my domain is domain.com
First, I'd like to force www. in the url with a 301 redirect.
Another thing, my website is not hosted in the root directory but in /laravel/public/
So I'd like to set this subdirectory as root, and remove it from the URL if someone try www.domain.com/laravel/public/ => www.domain.com
How can I do that?
Thanks in advance.
Assuming that you can't change the document root to point to your public folder, you can try adding these rules to document root (not your public folder):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /+lavarel/public/([^\?\ ]*)
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/laravel/public/
RewriteRule ^(.*)$ /laravel/public/$1 [L]
You'll need to have your own .htaccess file in the root of your domain, along with another in the laravel/public directory (there already is one there, but you need to modify it.
Below are the two files in full as I have them on my testing server.
/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Rewrite requests to the laravel/public index
RewriteCond %{REQUEST_URI} !^/laravel/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /laravel/public/$1 [L]
</IfModule>
/laravel/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Force www. (also strips laravel/public)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Strip laravel/public, prevent loops
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} ^/laravel/public(/.+)/? [NC]
RewriteRule ^ %1 [R=301,L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

htaccess - canonical URL when redirecting to subdirectory

I've been playing around with this problem for quite a while but can't find the way how to achieve what I want. I want user coming to my website test.com to reach index.php located in subdirectory www immediatelly, which I am able to do. But when the URL is test.com/www I want it to be just test.com.
Snippets of code:
.htaccess in /
RewriteRule ^(.*)$ /www/$1 [L]
.htaccess in /www
RewriteCond %{REQUEST_URI} ^/www.*$
RewriteRule ^/www/(.*)$ /$1 [L]
gives me 500 Internal Error.
UPDATE:
I came to a solution thanks to #Justin Iurman's answer:
.htaccess in /www
RewriteCond %{THE_REQUEST} \ /www/?(.*)\ HTTP/ [NC]
RewriteRule ^(.*)$ /%1 [R=301,L]
Have another problem tho. On the server machine I have multiple websites and I want to serve files according to HTTP_HOST variable in root .htaccess file. I redirect user accessing test.com to proper directory (say test), but I want URL test.com/test to redirect just to test.com. Directory test contains directory www where user is redirected and it does not stick to URL thanks to solution above. I would like to edit just .htaccess file in webserver root so I don't have any dependancy on websites' domains in projects.
SOLVED
.htaccess in webserver root:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect test.com/test (and everything into test folder) to test.com root
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteCond %{THE_REQUEST} \ /test/? [NC]
RewriteRule ^.*$ / [R=301,L]
# Forward (internally rewrite) test.com/one/example/page to test.com/test/www/one/example/page
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
.htaccess in the "test" directory:
<IfModule mod_rewrite.c>
RewriteEngine On
# allow /test in URL only for certain filetypes
RewriteRule ^(.*\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz))$ /test/www/$1 [L]
# allow /test on localhost
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
Thanks a lot Justin!
You have to avoid infinite loop when doing what you want.
Put this code in your htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} \ /www/?(.*)\ HTTP/ [NC]
RewriteRule . /%1 [R=301,L]
RewriteRule ^(.*)$ /www/$1 [L]
EDIT: taking your update into consideration, here's the new code
RewriteEngine on
# Redirect test.com/test (and everything into test folder) to test.com root
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteCond %{THE_REQUEST} \ /test/? [NC]
RewriteRule . / [R=301,L]
# Forward (internally rewrite) test.com/one/example/page to test.com/test/www/one/example/page
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]

Treat particular folder as actual folder not as controller CodeIgniter

I want to make a particular folder to be treated as an actual one, not as a controller in codeigniter and also want to redirect my all domain.com requests to www.domain.com. I have the following htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(FOLDER_TO_BE_EXCLUDED)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Redirect non-www to www:
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
If I uncomment the lines (Redirect non-www to www), all requests are treated as controller
so the folder (FOLDER_TO_BE_EXCLUDED) is also treated as controller. I want to access this folder as an actual one.
If I will not redirect non-www to www I cant access session variables on inner pages.
Hope you will understand.
RewriteCond only apply to the RewriteRule which follows: your rules (commented now) were incorrect.
Besides, IMO, the simplest way to define exceptions like this one, is to use a non-rewriting rule like: RewriteRule ^FOLDER_TO_BE_EXCLUDED/ - [L] (on "top" of rewrite rules)
RewriteEngine On
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}/$0 [R=permanent,L]
RewriteRule ^FOLDER_TO_BE_EXCLUDED/ - [L]
# Rewrite all other URLs to index.php/URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?url=$0 [PT,L]
Try adding a RewriteCond to your last RewriteRule:
RewriteCond $1 !^(folder_name/)
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
This should stop it redirecting www.yourdomain.com/folder_name to the index.php.
You can add more folders to the condition using the | character:
RewriteCond $1 !^(folder_name/|another_folder/)