Mod Rewrite Hide Folder - apache

I think this is a pretty simple question.
How do you an apache rewrite to hide a folder.
EX: www.website.com/pages/login.php to www.website.com/login.php
or www.website.com/pages/home.php to www.website.com/home.php
The folder needs to alway be hidden. thanks

I assume what you want is for the browser to request /home.php but the server to actually use the file located at /pages/home.php, right? If so, this should work:
Make sure the apache mod_rewrite module is installed. Then, use something like this in your apache config, virtual host config, or (less desirable) .htaccess file:
RewriteEngine On
RewriteRule ^/(.*)$ /pages/$1
The rules use regular expressions, so you may want to look at a reference on that topic if you're unsure. Read the manual for more info on other directives (RewriteCond can be very useful) or rule options.

I know the original post here was from a couple years ago, but it's been coming up first in the search engine, so maybe this will help others looking to hide a folder name in the URL.
Not exactly what original poster wanted, but along the same lines.
RewriteCond %{HTTP_HOST} ^mydomainname\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomainname\.com$
RewriteCond %{REQUEST_URI} !^/subfoldername/
RewriteRule (.*) /subfoldername/$1
The above example would redirect any request to mydomainname.com or www.mydomainname.com to the subfoldername directory in the root directory for the domain, and the subfolder name would not appear in the URL.

If your example actually reflects the files you need, then in your .htaccess file:
#Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/pages/(.+)\.php $1\.php [NC, L]
Also, if the directory has read permission, it cannot be, in reality "hidden". I assume you mean that it no longer appears in the url.

Related

Browser seems to show query given by .htaccess

There seems to be a problem with the approach I'm using to redirect everything to index.php which is in my root folder public_html. There should be no exceptions This is ofcourse, unless a folder turns RewriteEngine off. For simplicity lets just say I use www.example.com as my website.
My .htaccess looks as follows:
RewriteEngine on
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
As an example, let's say in the browser I enter: www.example.com/test
It redirects internally as intended to: www.example.com/index.php?url=test
The problem:
I would like the browser to simply still display: www.example.com/test
However, the browser now displays: www.example.com/test/?url=test
This messy name only shows up if I have a folder named test inside the root directory. If I delete the folder, it shows:
www.example.com/test
Yet, if I add the folder again, it shows:
www.example.com/test/?url=test
It still redirects properly, but it doesn't look clean anymore.
What I'm curious about is what exactly causes it to behave this way and how it can be solved/prevented.
Please, note that I always want to rewrite the url. So I do not want to make exceptions for folders. It should look and work the same no matter if there is a folder or not.
edit 1: The root folder would be public_html, so example.com (as an example)
edit 2: Changed phrasing of the question, the question is still the same.
edit 3: Changed the formatting of the question.
edit 4: Added that I want to rewrite the url, regardless whether there exist or doesn't exist a folder.
You need to use this in your .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /test/?url=$1 [L]
This code will leave you with your desired URL. Make sure you clear your cache before testing it.
This may need further edits to do exactly what you want, but using RewriteCond you can specify conditions on when to actually rewrite the url.
RewriteEngine On
RewriteBase /
##perform rewrite only if requested file does not exist...
RewriteCond %{REQUEST_FILENAME} !-f
##perform rewrite only if requested directory does not exist...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /test/?url=$1 [L]

.htaccess URL Rewrite not working

I've never been good at .htaccess, I'm trying to copy and paste some code that worked on another one of my domains and modify it to work here. I will have several rewritten URLs, some static, some dynamic, but I can't even get the simplest of them to work. This one is testable here: http://lindseymotors.com/home
Clearly, index.php is available because if you access http://lindseymotors.com it works.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.* [NC]
RewriteRule ^home$ index.php
RewriteRule ^home/$ index.php
# When answering, if you could write a statement that would combine
# both of the statements above into one that would be appreciated.
As I said, these same conditions worked on another one my domains because I copied the code right over. I asked my server admin to double check everything on his end and it was fine. Any ideas?
Only thing I can think of is make sure the use of .htaccess is really on. The easiest way you can check since your server admin says it's fine is to put random text at the top of your .htaccess file. If your .htaccess file is being read and .htaccess files are enabled, it should throw a 500 internal server error. If not, then they don't have .htaccess files enabled and need to add AllowOverride All to the Apache config vhost.
Here is your rule combined into one as you noted. You really don't need the RewriteCond, but I will leave since you were using it previously.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.* [NC]
RewriteRule ^home/?$ index.php [L]

Apache Rewrite: favicon in %{HTTP_HOST} directory

My software supports multiple domains pointed at the exact same directory, I use shared hosting. I need to have each domain's favicon load from directories with their respective host names. Here is a visual...
http://www.example1.com/favicon.ico
public_html/www.example1.com/favicon.ico
\
http://www.example2.com/favicon.ico
public_html/www.example2.com/favicon.ico
\
http://www.example3.com/favicon.ico
public_html/www.example3.com/favicon.ico
I've tried some rewrites along the lines like this without any success...
RewriteEngine on
RewriteRule ^favicon\.ico$ %{HTTP_HOST}/favicon\.ico
Things to keep in mind...
1.) I use shared hosting so remember that the answer I need should be short and simple.
2.) I will only accept a DYNAMIC answer, I will only use the %{HTTP_HOST} variable and NOT a static domain name as I will not be manually editing my .htaccess file every single time I add a domain name.
3.) I may end up putting a .htaccess file in those sub-directories though I do not at the moment, an exception for the favicon would be greatly appreciated though is not necessary for me to accept the answer.
4.) I'll be more than happy to make any clarifications.
Use this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^(favicon\.ico)$ %{HTTP_HOST}/$1 [L,NC]
I have been struggling with this issue too but I finally fixed it using the following rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/favicon\.ico$ /sites/all/themes/mytheme/favicon.ico
</IfModule>
I stuffed this into a virtual host declaration. You can do this for each of your virtual hosts, all you need to do is point the second part to the correct icon!
This solves all of my favicon problems, even for Firefox :)
(Tested on FF25, Safari 6.1, IE8 and IE10)
This one worked better in my case
RewriteCond $0 !=images/favicon.ico
RewriteRule ^([^/]+/)*favicon\.ico$ /images/favicon.ico [L,NC]
To support all possible browsers and platforms, in addition to favicon.ico file, need to have files such as android-chrome-192x192.png, apple-touch-icon.png, favicon-32x32.png, etc...
Here is rewrite rule to support them all:
RewriteRule ^(favicon.*\.(ico|png)|apple-touch-icon.*\.png|android-chrome.*\.png|mstile.*\.png|safari-pinned-tab.*\.svg)$ /favicons/%{HTTP_HOST}/$1 [L,NC]
This will serve favicons including Apple Touch, Android Chrome, Windows and other favicons from /favicons/<DOMAIN_NAME> folder.
I had a problem with favicon in files on subdomain
I was struggling with redirect for favicon in htaccess for only one subdomain for a long time.
My case was that all domain take favicon from public/ directory. One subdomain (let's call it 'subdomain_a') is configured to take it from another directory and it works.
Problem appeared when a file was opened on subdomain_a. The favicon in file view (f.e. pdf-viewer) was taken from public/ directory, not from configuration of subdomain_a.
Here is my solution:
# Redirect for favicon
RewriteCond %{HTTP_HOST} ^www.subdomain_a.com
RewriteCond %{REQUEST_URI} ^/favicon.ico$
RewriteRule ^(.*)$ /path/to/favicon/for/new/domain/$1 [R=301,L]

Apache .htaccess RewriteRule

Here's my situation. I have a web root and several subdirectories, let's say:
/var/www
/var/www/site1
/var/www/site2
Due to certain limitations, I need the ability to keep one single domain and have separate folders like this. This will work fine for me, but many JS and CSS references in both sites point to things like:
"/js/file.js"
"/css/file.css"
Because these files are referenced absolutely, they are looking for the 'js' and 'css' directories in /var/www, which of course does not exist. Is there a way to use RewriteRules to redirect requests for absolutely referenced files to point to the correct subdirectory? I have tried doing things like:
RewriteEngine on
RewriteRule ^/$ /site1
or
RewriteEngine on
RewriteRule ^/js/(.*)$ /site1/js/$1
RewriteRule ^/css/(.*)$ /site1/css/$1
But neither of these work, even redirecting to only one directory, not to mention handling both site1 and site2. Is what I'm trying possible?
EDIT: SOLUTION
I ended up adapting Jon's advice to fit my situation. I have the ability to programatically make changes to my .htaccess file whenever a new subdirectory is added or removed. For each "site" that I want, I have the following section in my .htaccess:
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{HTTP_COOKIE} sitename=site1
RewriteCond %{REQUEST_URI} !^/site1/
RewriteRule ^(.*)$ /site1/$1 [L]
Index.php is a file that lists all my sites, deletes the "sitename" cookie, and sets a cookie of "sitename=site#" when a particular one is selected. My RewriteConds check,
If the request is not for /
If the request is not for /index.php
If the request contains the cookie "sitename=site1"
If the request does not start with "/site1/"
If all of these conditions are met, then the request is rewritten to prepend "/site1/" before the request. I tried having a single set of Conds/Rules that would match (\w+) instead of "site1" in the third Condition, and then refer to %1 in the fourth Condition and in the Rule, but this did not work. I gave up and settled for this.
If the RewriteRules are in your .htaccess file, you need to remove the leading slashes in your match (apache strips them before sending it to mod_rewrite). Does this work?
RewriteEngine on
RewriteRule ^js/(.*)$ /site1/js/$1
RewriteRule ^css/(.*)$ /site1/css/$1
EDIT: To address the comment:
Yes, that works, but when I do RewriteRule ^(.*)$ /site1/$1, it causes Apache to issue internal server errors. But to me, it seems like that should just be a generic equivalent of the individual rules!
What's happening with that rule is when /something/ gets rewritten to /site/something/, and apache internally redirects, it gets rewritten again, to /site/site/something/, then again, then again, etc.
You'd need to add a condition to that, something like:
RewriteCond %{REQUEST_URI} !^/site/
RewirteRule ^(.*)$ /site/$1 [L]
You need to set up symlinks, which the rewrite rules will use so your absolute links at the server level can follow the symbolic links to the central site hosting account.

htaccess folder rewrite

I've been reading multiple posts on here about htaccess folder rewriting but none seem to fit my question (properly).
My question is:
I have 2 sub folders on the server, website1 and website2.
When a user goes to www.foo.com I wish the visual url to remain the same but want the server URI to go to /website1/ where it will load the index.php for website1
I then want the same thing only when a user goes to www.bar.com again the url does not change but this time it links to /website2/ where it will load the index.php for the 2nd website.
Would really appreciate some help with this as I'm still learning about rewrites. Examples with explanations would be highly appreciated. Also any advice of best practice (if their is any) would also be appreciated.
KingCrunch is right -- the proper way to setup such environment is to use <VirtualHost> directive in Apache config file.
If, for whatever reason this needs to be dona via rewrite and .htaccess .. then you need mod_rewrite to be enabled and .htaccess files to be allowed to contain rewrite rule (AllowOverride directive).
Here are the rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# rule #1
RewriteCond %{HTTP_HOST} =www.foo.com
RewriteCond %{REQUEST_URI} !^/website1/
RewriteRule (.*) /website1/$1 [L]
# rule #2
RewriteCond %{HTTP_HOST} =www.bar.com
RewriteCond %{REQUEST_URI} !^/website2/
RewriteRule (.*) /website2/$1 [L]
This code is to be placed in .htaccess file in root folder. If placed elsewhere (e.g. configuration or virtual host context) some tweaking may be required.
Fist rule is for www.foo.com and second for another domain name. These rules are pretty much the same. We tell Apache to check domain name (via {HTTP_HOST} request variable), and if it matches our domain rewrite (internal redirect) URL into one folder deeper. The second condition is to prevent a rewrite loop (to not to rewrite already rewritten URL). It is necessary as Apache, after executing rewrite, goes to the next rewrite iteration (that is how it works), and this condition is required to stop the loop.
Useful link: http://httpd.apache.org/docs/current/rewrite/
I believe that you need to use only RewriteCond and RewriteRule directives. Take a look 'Virtual User Hosts' at http://httpd.apache.org/docs/1.3/misc/rewriteguide.html.
The logical is the same. (I think.)