Failed to access subfolders | Php basics
I am editing a site built using some framework in PHP does not show which one. All public files like CSS, JS are in the public directory. I want to load a separate site in a domain like www.domain.com/newsite. I created a new site folder, but I can't access it 404 pages not found.
The htacess root is below
RewriteEngine on
RewriteRule ^(?!sitemap\.xml$).(.*)\.xml$ [R=404,L]
RewriteRule ^(?!robots\.txt$).(.*)\.txt$ [R=404,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
I created an anther folder inside the newsite public directory, which can now open it.
Htacess inside the public directory
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Any suggestions how I can access the folder directly
source to share
You only need to tell the root htaccess not to overwrite it in the directory public
when your new folder participates in the request. You currently have this piece of code
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
which means: overwrite the root page /
in a public
folder (first rule) and overwrite everything in a folder public
(second rule). Note that your first rule is useless.
Either way, you need to add a condition to achieve your goal
RewriteCond %{REQUEST_URI} !^/newsite
RewriteRule ^(.*)$ public/$1 [L]
which means: overwrite everything except newsite
folder to folder public
.
At the end, your htaccess should look like this:
RewriteEngine on
RewriteBase /
RewriteRule ^(?!sitemap\.xml$).(.*)\.xml$ [R=404,L]
RewriteRule ^(?!robots\.txt$).(.*)\.txt$ [R=404,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/newsite
RewriteRule ^(.*)$ public/$1 [L]
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
Pay attention to the presence RewriteBase
, which is always a good habit, especially when working with virtual directories created by some rules. Please also note that your two xml rules could be rewritten in a smarter / cleaner way.
source to share
Try using below rules,
RewriteEngine on
RewriteRule ^(?!sitemap\.xml$).(.*)\.xml$ [R=404,L]
RewriteRule ^(?!robots\.txt$).(.*)\.txt$ [R=404,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#Adding exception for newsite if not new site do rewrite to public/
RewriteCond %{REQUEST_URI} ^!newsite
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
You are getting not found error because you are currently internally rewriting everything to the public directory, try the new rules if you haven't.
source to share