.htaccess redirect does not working well - .htaccess

I want to redirect main domain "example.com" to to subdirectory "sub" with .htaccess. I have .htaccess file with this content:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/sub/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ sub/index.php [L]
and it works ok if I enter example.com or www.example.com in address bar in web browser, but if I enter example.com/index.php or www.example.com/index.php that's not working and it goes to primary folder, not in subfolder sub.
What I am doing wrong?

This works fine:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?example\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/sub/
RewriteRule ^(.*)$ /sub/$1 [L]
Also, you can use this for testing your htaccess

Why don't you set up your redirect in the index.php file? Your .htaccess file doesn't have a condition for that. Since you are hitting the page directly.
As per my comment below, put this into your index.php page:
header("Location: http://example.com/sub/index.php");
die();

Related

301 redirect from folder to subdomain, but remove the folder

I created subdomain from site.com/en/ to en.site.com using this:
RewriteBase /
RewriteCond %{HTTP_HOST} ^en.site.com$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/en(?:/|$)
RewriteRule ^(.*)$ en/$1 [L,QSA]
But when I add 301-redirect from site.com/en/ to en.site.com I got a loop. How to modify .htaccess to not have site.com/en/ on my site (but keep the info)?

.htaccess redirect from folder to file with variable

I'm wondering how I can redirect this:
sample.com/issues/3
sample.com/issues/4
etc.
to
sample.com/issues/index.php?id=3
sample.com/issues/index.php?id=4
etc.
UPDATE: A friend suggested this:
RewriteRule ^issues/(.*)$ issues/index.php?id=$1 [L]
But this redirects to the wrong subdirectory. E.g. it redirects /issues/4/ to /issues/4/?id=4
Strangely it seems to check out on this testing site but it's not working on my local server, nor on my live server.
Any ideas? The full .htaccess now looks like so:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sample.com [NC]
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,R=301]
Redirect /archive.php /issues/
RewriteRule ^issues/(.*)$ issues/index.php?issue=$1 [L]
Create a new .htaccess inside issues/ directory with this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?issue=$1 [L,QSA]

Two rewrite rules in the same htaccess

In the www root folder of my web server I have 2 folders for 2 differents websites
www/firstsite
www/secondsite
To reach the index.php file of the first site I go to www.firstsite.com/firstProject
To reach the index.php file of the second website I go to www.secondsite.com/secondProject
I would like to put an unique htaccess file in the root folder "www". This htaccess file would have a rewriterule for both webstie.
So users would see www.firstsite.com instead of www.firstsite.com/firstProject
and www.secondsite.com instead of www.secondsite.com/secondProject
Edit: I use this htaccess to rewrite the url of www.firstsite.com(don't forget this website is located in folder www/firstsite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ firstsite/$1 [QSA,L]
</IfModule>
Edite: here is the answer thank you to hjpotter92 !
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?firstsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/firstProject [NC]
RewriteRule ^(.*)$ /firstProject/$1 [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?secondsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/secondProject [NC]
RewriteRule ^(.*)$ /secondProject/$1 [L]
Use the %{HTTP_HOST} variable to test your domain name, and redirect based on that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?firstsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/firstProject [NC]
RewriteRule ^(.*)$ /firstProject/$1 [L]
similar rules will go for your second domain.

Two htaccess files in control of one website

When I go to
www.mydomain.tld/sk/
everything works fine, link is not changed.
But when I delete forward slash
www.mydomain.tld/sk
the page is changed to
www.mydomain.tld/domains/mydomain.tld/sk/?lang=sk
Can anybody help please? How can avoid the link to be changed?
When I go to www.mydomain.tld/sk I need it to remain the same.
.htaccess file (1):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
RewriteRule ^sk$ /index.php?lang=sk [L,QSA]
RewriteRule ^sk/$ /index.php?lang=sk [L,QSA]
RewriteRule ^sk/index /index.php?lang=sk [L,QSA]
There is another one .htaccess file (2) in the top main directory
and it contains these lines
RewriteEngine On
RewriteCond %{REQUEST_URI} !^domains/
RewriteCond %{REQUEST_URI} !^/domains/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/domains/%2 -d
RewriteRule (.*) domains/%2/$1 [DPI,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
This .htaccess file helps to navigate domains that I am running at server.
All web sites are in a directory /domains .
So for example www.mydomain.tld is pointing to /domains/mydomain.tld directory.
In this directory is the shorter htaccess file (1).
Not so easy, but I think it's because in .htaccess file (1) your link are from the root, and are not at root...
Try with: .htaccess file (1):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
RewriteRule ^sk/?$ index.php?lang=sk [L,QSA]
RewriteRule ^sk/index index.php?lang=sk [L,QSA]
look at
RewriteCond %{REQUEST_URI} !^domains/
RewriteCond %{REQUEST_URI} !^/domains/
here are you testing if REQUEST_URI does not start with "domains" OR "/domains"
however in the previous htaccess, you guarantee that REQUEST_URI will start with www
you probably want REQUEST_FILENAME instead of REQUEST_URI here
I spent many hours by trying to solve it in htaccess, but nothing work.
So i just 301 redirect the wrong link in php:
if ($_SERVER['REQUEST_URI']=="/domains/mydomain.tld/sk/?lang=sk") {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.mydomain.tld/sk/");
exit();
}

htaccess redirect http to https for subdirectory

I use this redirect because my website is inside a subfolder (www.domain.com/subfolder/)
So if someone enters the website, it will load like this: http://domain.com
What do I need to add or change in .htaccess so that it redirects automatically to https://domain.com, counting that the website is at domain.com/subfolder
I have tried various redirect codes, but no success. If someone is kind to point me in the right direction or give me a hint.
Thanks for the help.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ subfolder/index.html [L]
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule !^subfolder/ subfolder%{REQUEST_URI} [L,NC]

Resources