I am editing a site that is made using some framework on PHP doesn't show which.
All the public files like CSS,JS are inside a Public directory. I want to upload a separate site within the domain like www.domain.com/newsite i have created the folder new site but i am not able to access it it goes to 404 page not found.
the Htacess of the root is as 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 anther folder inside public directory newsite still now able to open it.
Htacess inside public directory is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Any suggestions how can i access the folder directly
You only need to tell the root htaccess not to rewrite to public directory when your new folder is involved in the request. Currently, you have this part of code
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
which means: rewrite root page / to public folder (first rule) and rewrite everything to public folder as well (second rule). Note by the way that your first rule is then useless.
Anyway, you need to add a condition to reach your goal
RewriteCond %{REQUEST_URI} !^/newsite
RewriteRule ^(.*)$ public/$1 [L]
which means: rewrite everything except newsite folder to public folder.
At the end, your root 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>
Note the presence of RewriteBase which is always a good habit, especially when dealing with virtual directories created by some rules. Note also that your two xml rules could be rewritten in a more clever/cleaner way
Try to add
RewriteBase /newsite/
Write this line under "RewriteEngine On"
Try to use 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 public directory please try the new rules if you haven't.
Related
I've used the following rules in my htaccess file in other applications to redirect users from a folder to a subdomain but load the content from that folder when accessing that subdomain.
# REWRITE SUBDOMAIN TO FOLDER
RewriteCond %{HTTP_HOST} ^admin\.cameron\.com$
RewriteRule !^admin/? admin%{REQUEST_URI} [NC,L]
# REWRITE FOLDER TO SUBDOMAIN
RewriteCond %{THE_REQUEST} \s/admin/([^\s]*) [NC]
RewriteRule ^ http://admin.cameron.com/%1 [R=301,L]
So if I go to: http://cameron.com/admin I end up on http://admin.cameron.com/
But the content is loaded from http://cameron.com/admin
However this doesn't work for CakePHP 2.x because of its rewriting apparently...
In my htaccess file in /app/webroot I have:
<IfModule mod_rewrite.c>
RewriteEngine On
# CAKEPHP RULES
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
# REWRITE SUBDOMAIN TO FOLDER
RewriteCond %{HTTP_HOST} ^admin\.cameron\.com$
RewriteRule !^admin/? admin%{REQUEST_URI} [NC,L]
# REWRITE FOLDER TO SUBDOMAIN
RewriteCond %{THE_REQUEST} \s/admin/([^\s]*) [NC]
RewriteRule ^ http://admin.cameron.com/%1 [R=301,L]
</IfModule>
If I go to: http://cameron.com/admin it just tries to load the AdminController and doesn't redirect you, and if I go to: http://admin.cameron.com/ I just get a 500 Internal Server Error.
Any ideas on how to get this working for CakePHP?
CakePHP and almost any other PHP framework parse $_SERVER['REQUEST_URI'] in order to route your request to particular controller
more info here: https://github.com/cakephp/cakephp/blob/2.6/lib/Cake/Network/CakeRequest.php#L230 ,
so you simply need to have the SAME request URI parameters for your redirect to get app working.
For your old location it was "/admin", for new location it should be the same, but you do not want to pass it, so it is better to change the task like this:
"When you go to admin.site.com you will be redirected to site.com/admin".
This can be done simply like this:
RewriteRule ^([^.]+)\.example\.com http://example.com/$1
It is not a working example of rule because you also need to rewrite subdomain's request URI to to get everything working and it depends on your requirements, but can be smth like this:
RewriteRule ^([^.]+)\.example\.com(.*) http://example.com/$1/$2
UPD: real example
$ cat app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} ^admin.example.com
RewriteRule ^(.*)$ http://example.com/admin/$1 [P,L,NC]
</IfModule>
<IfModule mod_rewrite.c>
#standard cake htaccess stuff
...
RewriteCond %{HTTP_HOST} ^profile\.thechatfun\.com$ [NC]
RewriteRule ^(.*)$ http://www.thechatfun.com/users/profile/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^chat\.thechatfun\.com$ [NC]
RewriteRule ^(.*)$ http://www.thechatfun.com/chats/index/$1 [R=301,L]
</IfModule>
I have a standard Larvael folder structure, and I want to have a Wordpress blog in /blog folder.
I am on a shared hosing, so I am using this htaccess at root of my domain to rewrite all requests to /public:
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
Then I thought I'd put blog in the root and just not do the rewriting if the route starts with blog:
RewriteCond %{REQUEST_URI} !^public
RewriteCond %{REQUEST_URI} !^blog
RewriteRule ^(.*)$ public/$1 [L]
But that didn't work.
How can I achieve this?
Thanks,
Petar
And with that:
RewriteCond %{REQUEST_URI} !^/public
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.*)$ public/$1 [L]
Because URI begin with /
My application is quite large and it is split into areas and I would like to have a seperate public folder in each of these areas for images and scripts that are only needed within that specific area.
I am currently directing all traffic to my public folder with the following:
RewriteEngine On
RewriteBase /
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
And in the public folder I have the following .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Is it possible to allow requests to:
http://example.com/area/public/scripts/myscript.js
Change your root .htaccess to this:
RewriteEngine On
RewriteBase /
RewriteRule ^$ public/ [L]
RewriteRule ^((?!area/public/).*)$ public/$1 [L,NC]
This is similar to question mod_rewrite: Being redirected to root folder; I want it to stay in sub-folder
but the answer there is not working for me.
I have a main site which redirects via an index.php handler, but I want a pre-live test area which is held in a subdirectory /UA/
I need requests for UA/ to pass through and be handled in the sub directory, I have tried both RewriteRule ^/UA - [L] and RewriteCond %{REQUEST_URI} !^/UA in my .htaccess of the main directory but this hasn't worked. I've tried with/without /'s
Here are my two .htaccess files:
RewriteEngine on
ReWriteBase /
# Redirect to HTTPS site
RewriteCond %{HTTP_HOST} ^my\-site\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.my\-site\.com$
RewriteRule ^/?$ "https\:\/\/www\.mysite\.com\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteRule ^/?$ "https\:\/\/www\.mysite\.com\/" [R=301,L]
#Allow UA access
RewriteRule ^/UA - [L]
#Allow certain file types to access directly
RewriteRule \.(css|js|png|jpg|gif|woff|eot|ttf|svg|ico)$ - [L]
# Catch all
RewriteCond %{REQUEST_URI} !^/UA
RewriteRule .* index.php [L]
RewriteEngine on
ReWriteBase /UA/
#Allow certain file types to access directly
RewriteRule \.(css|js|png|jpg|gif|woff|eot|ttf|svg|ico)$ - [L]
# Catch all
RewriteRule .* index.php [L]
The problem is that the site continues to direct to the Live index.php version not the one in /UA/ with a URI of https://www.mysite/UA/
Add this line to always have directory slash on in your root .htaccess:
DirectorySlash On
Keep /UA/.htaccess like this:
RewriteEngine on
ReWriteBase /UA/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif|woff|eot|ttf|svg|ico)$ [NC]
RewriteRule ^ index.php [L]
Here is my site url structure as follows: http://www.sitename.com/new/about-us .
What I really want to do now is hide the directory name 'new' from the above url, but the admin url should remain unchanged.
The admin url would be like : http://www.sitename.com/new/admin .
My previous .htaccess code as follows:
RewriteEngine On
RewriteBase /new/
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/new/$1 [R=301,L]
RewriteRule blog/ - [L]
RewriteRule (^wlp) - [L]
RewriteRule admin/ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %(REQUEST_FILENAME} !-d
RewriteRule ^([\S\s/+.]+)/?$ index.php?url=$1 [QSA,L]
Here is my server directory structure:
/public
/new
.htaccess
index.php
about-us.php
/blog
/admin
Any help in this regard will be highly appreciated.
It sounds like what you are trying to accomplish is a common technique which I've seen before for forwarding all public_html requests to public_html/public to essentially hide the contents of the public_html directory from the user and making public_html/public the new web root.
Try using this in your public_html/.htaccess file (you can write further htaccess in public_html/new/.htaccess):
RewriteEngine on
RewriteBase /
#enforce www subdomain
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^sitename.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#forward all requests, except new/admin, to the 'new' directory without the user's awareness
RewriteRule new/admin - [S=2]
RewriteRule ^$ new/ [L]
RewriteRule (.*) new/$1 [L]
This .htaccess in / should do:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^new/
RewriteRule ^(.*)$ new/$1 [L]