Combine .htaccess files to a single working one - .htaccess

I have .htaccess files on every subfolder in demo.mydomain.net to redirect to mydomain.net without redirecting image/js files.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/load
RewriteRule ^/?$ "http\:\/\/mydomain\.net" [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/load/images
RewriteRule ^/?$ "http\:\/\/mydomain\.net" [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/load/scripts
RewriteRule ^/?$ "http\:\/\/mydomain\.net" [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/load/styles
RewriteRule ^/?$ "http\:\/\/mydomain\.net" [R=301,L]
Is there any way I can combine these to a single working file and save it to where my index file is?

You would need the following combined rule within the .htaccess in your directory mapped to demo.mydomain.net
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/load(/(images|scripts|styles))? [NC]
RewriteRule ^ http://mydomain.net [R=301,L]
The above %{REQUEST_URI} regex intercepts all four URL paths.

Related

htaccess redirect all domain except one subfolder including the files and other subfolders inside

I'm currently using:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder/.*
RewriteCond %{REQUEST_URI} !^/folder$
RewriteRule .* https://www.newdomain.com/ [R=301,L]
Which works great at redirecting the "folder" to the "newdomain," including the files within that folder, however, the subfolders within that folder still redirect.
I've tried:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder/.*
RewriteCond %{REQUEST_URI} !^/folder$
RewriteCond %{REQUEST_URI} !^/folder/subfolder/.*
RewriteCond %{REQUEST_URI} !^/folder/subfolder$
RewriteRule .* https://www.newdomain.com/ [R=301,L]
As well as:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder/.*
RewriteCond %{REQUEST_URI} !^/folder$
RewriteCond %{REQUEST_URI} !^/subfolder/.*
RewriteCond %{REQUEST_URI} !^/subfolder$
RewriteRule .* https://www.newdomain.com/ [R=301,L]
with no luck.
Any suggestions?
You can use the following
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/subfolder.*/$ [NC]
RewriteRule .* https://www.newdomain.com/ [R=301,L]
Make sure to clear your browser cache before testing this.

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.

Htaccess rewriterule, file was not found on this server

I am new to the whole htaccess language, but I feel like I am close to what I want.
Facts:
parts is a real folder/directory
anotherpart is a real folder/directory
there are no other files in <root> other then index.php
there are no files in the parts folder/directory
Folder/Directory structure:
<root>/index.php
<root>/parts/anotherpart/this.php
What .htacces I have running:
RewriteEngine On
RewriteBase /
RewriteRule ^parts/anotherpart/(.*)$ /$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.somewebby.it$ [NC]
RewriteRule ^(.*)$ https://somewebby.it/$1 [R=301,L]
Results:
always having https and no www
hiding parts/anotherpart and showing https://somewebby.it/this.php
The problem:
The requested URL /this.php was not found on this server.
You can replace your current code by this one in your htaccess (which has to be in root folder)
RewriteEngine On
RewriteBase /
# if "www" or http -> redirect to https://domain.com/xxx
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://somewebby.it/$1 [R=301,L]
# hide "parts/anotherpart/"
RewriteCond %{THE_REQUEST} \s/parts/anotherpart/([^\s]+)\s [NC]
RewriteRule ^ %1 [R=301,L]
# silently rewrite back to "parts/anotherpart/"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/parts/anotherpart/$1 -f
RewriteRule ^(.+)$ parts/anotherpart/$1 [L,QSA]

disabled htaccess for selected directory

i have a htaccess code shown below:
RewriteEngine On
RewriteCond $1 !^/administrator
RewriteCond %{HTTP_HOST} ^localhost/host
RewriteRule (.*) http://localhost/host/$1 [R=301,L]
RewriteRule ^([-\w]+)?/?([-\w]+)?/?([-\w]+)?/?$ index.php?seg1=$1&seg2=$2&seg3=$3
but it still read the htaccess in administrator folder. is the code incorrect?
Thanks.
RewriteCond statements apply to one RewriteRule only.
You need to repeat your RewriteConds for the second rewrite rule (and use REQUEST_URI):
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/administrator
RewriteCond %{HTTP_HOST} ^localhost/host
RewriteRule (.*) http://localhost/host/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/administrator
RewriteCond %{HTTP_HOST} ^localhost/host
RewriteRule ^([-\w]+)?/?([-\w]+)?/?([-\w]+)?/?$ index.php?seg1=$1&seg2=$2&seg3=$3

Remove www. in directory only

I'm trying to remove the www. for a single directory called dir (for example). I need to be able to do this from the .htaccess file in that directory. (I don't have root access.) Any idea how to make this work?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com/dir$ [NC]
RewriteRule ^(.*)$ http://example.com/dir$1 [R=301,L]
Update—the solution:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/dir/$1 [R=301,L]
The HTTP_HOST will not contain the path being accessed you need to match that in the rewrite rule itself:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^dir\/(.*)$ http://example.com/dir/$1 [R=301,L]

Resources