How to hide directory name from URL using .htaccess - php

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]

Related

Not able to access Subfolders | Php framework

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.

CakePHP subdomains with htaccess

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>

htaccess in subdirectory for parked domain

Motive: What I am trying to do is use parked domain and redirect it to subdirectory. and in subdirectory I have a script that needs pretty url's and htaccess is the only thing I can use.
Code in main .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^info/(.+)$ contact.php?a=$1
RewriteRule ^list/(.+)/page/(.+)$ list.php?a=$1&p=$2 [L]
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/m/$1 [R=301]
Code in /m .htaceess
RewriteEngine On
RewriteBase /
RewriteRule ^how/(.+)$ how.php?a=$1
RewriteCond %{HTTP_HOST} ^(www.)?main-example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/m/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
What i want is to achieve when user visits www.example.com/m/how/love the script should treet a get variable of a something like www.example.com/m/how.php?a=love but url should
remain pretty.
What my current code does is unlimited redirects to /m/404.shtml giving 301 in firebug.

How to load files from a subfolder instead of root folder in htaccess?

my test website is frontshoppe.com
and I have the following .htaccess below.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^frontshoppe\.com$
RewriteRule (.*) http://www.frontshoppe.com/$1 [R=301,L]
RewriteRule ^$ admin [L]
Yes it is loading fine like i wanted it to load the contents of the admin folder..
But I would also like the url to be..
www.frontshoppe.com ONLY.. and not
www.frontshoppe.com/admin
This should work:
RewriteEngine on
# Change yourdomain.com to be your primary domain.
RewriteCond %{HTTP_HOST} ^(www.)?frontshoppe.com$
# Change 'subfolder' to be the folder you will use for your primary domain.
RewriteCond %{REQUEST_URI} !^/admin/
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subfolder' to be the folder you will use for your primary domain.
RewriteRule ^(.*)$ /admin/$1
# Change yourdomain.com to be your primary domain again.
# Change 'subfolder' to be the folder you will use for your primary domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?frontshoppe.com$
RewriteRule ^(/)?$ admin/index.php [L]
You can have your rules like this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^frontshoppe\.com$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+admin/(\S*) [NC]
RewriteRule ^ /%1 [L,R=302,NE]
RewriteRule ^((?!admin/).*)$ admin/$1 [L,NC]

.htaccess pass request on for /UA/ folder

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]

Categories