Redirecting site to sub-sub folder - regex

I have a website in my public_html folder placed in the following folder structure
public_html/dps/main
I am using .htaccess to redirect in the following format ;
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?website.co.uk$
RewriteCond %{REQUEST_URI} !^/dps/main/
RewriteCond /domain/%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /dps/main/$1/
RewriteCond %{HTTP_HOST} ^(www.)?website.co.uk$
RewriteCond %{REQUEST_URI} !^/dps/main/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dps/main/$1
RewriteCond %{HTTP_HOST} ^(www.)?website.co.uk$
RewriteRule ^(/)?$ /dps/main/index.php [L]
Unfortunately it gives me Internal Server Error
not sure what is wrong with this code.
Thank you for your help in advance

Replace your rules with this:
DirectoryIndex index.php
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?website\.co\.uk$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!dps/main/).*)$ /dps/main/$1 [L,NC]

Related

How can I make multiple permalinks in .htaccess?

The problem is that I have a .htaccess file which redirects users that go to example.com/f89sk3 -> example.com/?s=f89sk3 if it makes any sense.
I want the same thing to happen for people that go to for example:
example.com/p/login -> example.com/p/login
This is my current .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?s=$1 [QSA,L]
You can use these rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^p/(.+)$ index.php?p=$1 [QSA,L]
RewriteRule ^(.+)$ index.php?s=$1 [QSA,L]

Redirect to internal web-directory

I already found this script to "change the documentroot":
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.your-domain-name.com$
RewriteCond %{REQUEST_URI} !new-folder/
RewriteRule (.*) /new-folder/$1 [L]
It's simple and cool, but I want to complement the script. I want to redirect to the file /new-folder/index.php if the requested file does not exist inside the directory /new-folder/, so not only %{REQUEST_URI} !-f!
But I don't know how to get the current script-path, like __DIR__ (PHP) in .htaccess.
So my question is, how to check if a file exists inside a subdirectory (new-folder)?
My redirect will theoretically look like:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.your-domain-name.com$
RewriteCond %{REQUEST_URI} !./new-folder/
RewriteCond (PATH+Filename, if no filename is set, take index.php) !-f
RewriteRule (.*) ./new-folder/index.php [L]
The above code uses ./ to check relative, it does not know if it's inside the root!
Also I would like to manipulate the REQUEST_URI: replace the /new-folder/ from it, so scripts inside this folder don't have to do it
You can use it like this in your root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?your-domain-name\.com$ [NC]
RewriteCond %{REQUEST_URI} !/new-folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/new-folder/$1 !-f [NC]
RewriteCond %{DOCUMENT_ROOT}/new-folder/$1 !-d [NC]
RewriteRule (.*) /new-folder/index.php [L]
Another option is use this in any sub-directory /anydirectory/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}new-folder/$1 !-f [NC]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}new-folder/$1 !-d [NC]
RewriteRule (.*) %{ENV:BASE}new-folder/index.php [L]

.htaccess make subdirectory root for domain

I am trying to use htaccess to make a subdirectory the root for an additional domain. I currently have:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?thedrive.co$
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/$1
RewriteCond %{HTTP_HOST} ^(www.)?thedrive.co$
RewriteRule ^(/)?$ test/index.php [L]
It works great and hides the 'test' directory in the URL. But, when a file in a subdirectory is requested it shows the 'test' directory. For example, instead of thedrive.co/somefolder it shows thedrive.co/test/somefolder.
How can I get it to not display that?
Insert a 301 rule right at the top below RewriteEngine on line:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?radioreformation\.co$ [NC]
RewriteCond %{THE_REQUEST} \s/+radioreformation.com/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?radioreformation.co$
RewriteCond %{REQUEST_URI} !^/radioreformation.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /radioreformation.com/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?radioreformation.co$
RewriteRule ^(/)?$ radioreformation.com/index.php [L]
This will redirect thedrive.co/test/somefolder to thedrive.co/somefolder

multiple .htaccess files conflicting with each other

I have a shared hosting with multiple domain hosted on it. In the root folder of my hosting exists a .htaccess (say is htaccess1). Code in ht1 is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydomain/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteRule ^(/)?$ mydomain/index.php [L]
These rules are there so that only urls with www.mydomain.com use all files of mydomain folder. I am new to url rewriting so I don't understand the meaning of what each line does. Another .htaccess (say htaccess2) file exists in mydomain folder. Code is htaccess2 is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)?/$ try.php?id=$1
RewriteRule ^([0-9]+)?$ try.php?id=$1
RewriteRule ^post/([0-9]+)?/$ post.php?id=$1
RewriteRule ^post/([0-9]+)?$ post.php?id=$1
In this file, line 6,7 works fine and redirect requests with numeric parameters to try.php but lines 8,9 doesn't work and gives a 404 page not found error, exact error string is :
The requested URL /mydomain/post/1233445 was not found on this server.
I doubt that some effect of htaccess1 is creating this problem as I tried many variations of regex in line 8,9. Please help.
Main .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydomain/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^$ /mydomain/index.php [L]
Changes:
Added RewriteBase at the top.
Added L flag to mark it last rule.
Added required options.
mydomain .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /mydomain/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^post/([0-9]+)/?$ post.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ try.php?id=$1 [L,QSA]
Changes:
Added RewriteBase at the top.
Added L flag to mark it last rule.
Removed a redundant rule.
Moved specific rule above the generic rule.
Added required options.

htaccess rewriterule redirect everypage to a single page

I want to redirect every url to a single php file that will act as a dispatcher that will pull content from a DB based on the url. I am unsure what is wrong with this. Any pointers please.
<IfModule mod_rewrite.c>
AddDefaultCharset utf-8
rewriteCond %{REQUEST_URI} !(^/admin/)
rewriteCond %{REQUEST_URI} !(\.css$)
rewriteCond %{REQUEST_URI} !(/robots\.txt$)
rewriteCond %{REQUEST_URI} !(\.png$)
rewriteCond %{REQUEST_URI} !(\.jpg$)
rewriteCond %{REQUEST_URI} !(\.jpeg$)
rewriteCond %{REQUEST_URI} !(\.pdf$)
rewriteCond %{REQUEST_URI} !(\.gif$)
rewriteCond %{REQUEST_URI} !(\.GIF$)
rewriteCond %{REQUEST_URI} !(\.xml$)
rewriteCond %{REQUEST_URI} !(\.js$)
rewriteCond %{REQUEST_URI} !(\.ico$)
RewriteRule . pageDispatcher.php [L]
Change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^admin/)^.*$ pageDispatcher.php [L]