How to rewrite requests to index.php to parent directory - regex

I have a website with two .htaccess files one in the projects root directory and one in the /Project Root/Public/ directory and I want to rewrite direct requests to "http://localhost/Project Root/index.php" to "http://localhost/Project Root/" how would I go about doing this?
My current .htaccess(s):
Project Root .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
/Project Root/Public/.htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} \s(?:/+(.+/))?public/ [NC]
RewriteRule ^ /%1 [L,R=301,NE]
DirectoryIndex index.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.php/
</IfModule>
</IfModule>

Have your Project Root .htaccess like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+([^/]+/)?index\.php[?\s] [NC]
RewriteRule ^ /%1 [L,R=301,NC,NE]
RewriteRule .* public/$0 [L]
Make sure you clear your browser cache before testing.

Related

Non https main site redirects to https with directory

I'm using this piece of code to redirect all http to https in my .htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Works great. But my only problem is if I access the root non http
http://example.com redirects to https://www.example.com/dev/
/dev is the directory where the main site is located.
my cpanel directory is like this
public_html/dev <---- https://www.example.com
How can I fix this issue. It should redirect to https version without the dev directory.
Hope someone could shed a light on this. thanks.
EDIT:
To make it more clear here's the directory structure
The contents of .htaccess in public_html is
public_html/.htaccess
rewritecond %{HTTP_HOST} ^example.com$ [NC,OR]
rewritecond %{HTTP_HOST} ^www.example.com$
rewritecond %{REQUEST_URI} !dev/
rewriterule (.*) /dev/$1 [L]
RewriteCond %{HTTP_HOST} ^oldurl\.com$ [OR] // if old url redirect to example.com
RewriteCond %{HTTP_HOST} ^www\.oldurl\.com$ // if old url redirect to example.com
RewriteRule ^/?$ "https\:\/\/www\.example\.com" [R=301,L]
here's the contents of .htaccess file in public_html/dev
public_html/dev/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Have it this way.
public_html/.htaccess
RewriteCond %{HTTP_HOST} ^(?:www\.)?oldurl\.com$ [NC]
RewriteRule ^/?$ https://www\.example\.com [R=301,L]
# enforce HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# First add a trailing slash if dev/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/dev/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301,NE]
# forward everything to /dev directory
RewriteCond %{REQUEST_URI} !^/dev/ [NC]
RewriteRule .* dev/$0 [L]
public_html/dev/.htaccess:
RewriteEngine On
RewriteBase /dev/
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Make sure to clear your browser cache completely before testing this changge.

Forcing http to https append "public" in URL

I have the following .htaccess inside webroot directory:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(/admin/)
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
Then, inside public directory I have the following .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?$1 [QSA,L]
# Force HTTPS
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Then, if I hit the following URL:
http://example.com/
The browser is redirected to:
https://example.com/public/
When site is loaded over HTTP (without forcing), this part is omitted.
Force https rule must be placed in root .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# Force HTTPS
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^$ public/ [L]
RewriteCond %{REQUEST_URI} !^(/admin/)
RewriteRule (.*) public/$1 [L]
</IfModule>
Remove http->https rule from public/.htaccess and clear browser cahce to test this.

htaccess URL rewrite - requested URL not found on server

Need to hide one folder while loading like Uniimart1609/S/page.php to Uniimart1609/page.php
in linux
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule Home_Page.php$ S/Home_Page.php
</IfModule>
is redirected http://Uniimart1609/Home_Page.php this is working fine, but when i rewrite the htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /S/$1.php [L,NC,QSA]
</IfModule>
getting The requested URL /S/Home_Page.php was not found on this server. Pleas help
Have your rule like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /Uniimart1609/
RewriteCond %{THE_REQUEST} /S/(\S*) [NC]
RewriteRule ^ %1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/S/$1\.php -f [NC]
RewriteRule ^([^./]+)/?$ S/$1.php [L]
</IfModule>

Redirect Not Working?

So My problem is that when visiting example.com/members I am redirected to www.example.commembers/ (notice that this is not a valid web address) instead of members.example.com (It only works correctly when there is the www). However, this redirection seems to happen with or without the following htaccess file. What to I need to add/change to stop this problem.
This is my htaccess file located in the members folder:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/members/(.*)$ http://members.example.com/$1
Then there is the wordpress htaccess file in the root directory:
# 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
Problem is in your first rule. Change your code to:
RewriteEngine On
RewriteBase /members/
RewriteCond %{HTTP_HOST} !^members\. [NC]
RewriteRule ^(.*)$ http://members.example.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

How to configure htaccess file for Cake 2.3.x on 1and1 shared hosting

Using the default cakephp htaccess file setup will not work on my domain when I want to install my Cakephp app in a subfolder, while everything works on localhost (xampp)
target => http://example.com/mycakeapp
Install needs 3 htaccess files:
root .htaccess
#.htaccess in root
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /mycakeapp
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
In app .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /mycakeap
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/ webroot/$1 [L]
</IfModule>
In webroot .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mycakeapp
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Following CakePHP's documentation, and Using these htaccess files, I get error500 results.
Using RewriteBase / instead of /mycakeapp will throw 404 error page.
PHP is in 5.4 version. How can I solve this?
/dirCakePhp
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ /app/webroot/ [L]
RewriteRule (.*) /app/webroot/$1 [L]
</IfModule>
/direCakePhp/app
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ /webroot/ [L]
RewriteRule (.*) /webroot/$1 [L]
</IfModule>
/direCakePhp/app/webroot
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
Juste add '/' after RewriteRule,
And change PHP version in 1and1 hosting panel to => 5.2
Add date_default_timezone_set('Europe/Paris'); in core.php
Setup your rules like this:
.htaccess in DOCUMENT_ROOT
RewriteEngine on
RewriteBase /
RewriteRule (.*) mycakeapp/$1 [L]
.htaccess in DOCUMENT_ROOT/mycakeapp
RewriteEngine on
RewriteBase /mycakeapp/
RewriteRule (.*) app/webroot/$1 [L]
.htaccess in DOCUMENT_ROOT/mycakeapp/app
RewriteEngine on
RewriteBase /mycakeapp/app/
RewriteRule (.*) webroot/$1 [L]
.htaccess in DOCUMENT_ROOT/mycakeapp/app/webroot
RewriteEngine On
RewriteBase /mycakeapp/app/webroot/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]