.htaccess URL Rewriting for Folders - .htaccess

I have an app located at example.com/app with signin.php, signup.php, index.php, etc inside the folder.
Now the client wants to have the app accessible at example.com/signin.php and so on, but I want to keep the app files inside the app folder for better management.
I tried using this, and it works, but then I can't access the files that I already had in the root directory:
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule (.*) /app/$1
Is there a way to exclude pages in the root directory from being affected? Or is there a better way to do this?

Can't you just get you http server to point tot he app folder as the base?

Consult the documentation of apaches mod_rewrite, look at the RewriteMap directive. Using that you can easily name all names you want to or you do not want to be rewritten.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule (.*) /app/$1
Using -f and -d makes sure it doesn't mess up any existing files or directories.

Related

Simple mod_rewrite rules broken after new c9 version

I'm working on a project hosted on the Cloud9 IDE. I had a simple set of mod_rewrite rules set up, but they no longer work after c9's new version rollout. It took me forever to iron out these rules (I'm a novice at best at this) I'm confused as to why these rules no longer work (AFAIK, the new c9 version should not have affected mod_rewrite rules).
Here are the rules (located in the root .htaccess)
RewriteEngine on
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_URI} ^/css/.*$ [OR]
RewriteCond %{REQUEST_URI} ^/img/.*$ [OR]
RewriteCond %{REQUEST_URI} ^/js/.*$
RewriteCond Astralis/resources%{REQUEST_URI} -f
RewriteRule ^(.+)$ Astralis/resources/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The goal is pretty straightforward... All requests going to /css/... /img/... or /js/... should serve up the associated file within Astralis/resources (after checking that the file exists). Otherwise, redirect the rest of the traffic to index.php.
The problem I am experiencing is that all requests to resources (css, img, js) are returning 404's. If I move the /css, /img, and /js folders from within Astralis/resources back to the root directory, all the resources load properly. This problem started happening after c9's new version, with no changes to the .htaccess file, the codebase, or directory structure.
Any clue as to what is going on? How do I debug this kind of stuff? Any general tips/tricks for writing mod_rewrite rules would also be appreciated. Thanks.
Since Apache needs full path of the file in order to return true using -f you will need to use %{DOCUMENT_ROOT}/ before your file path.
Have it this way:
RewriteEngine on
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(css|img|js)/ [NC]
RewriteCond %{DOCUMENT_ROOT}/Astralis/resources%{REQUEST_URI} -f
RewriteRule ^(.+)$ Astralis/resources/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

What all do I need in my .htaccess, such as permissions, etc

I've got a .htaccess file at the root directory of my website, and I've made sure .htaccess is the extension, not .htaccess.txt. I'm trying to clean up my .php urls, and even after looking at all the other questions about this, it is not working.
I'm quite strongly sure that the code for removing the extension is correct, so it makes me wonder if there is other code that I need inside the .htaccess for permissions or something like that. Thanks for any answers, and please don't mark this as duplicate... I've searched for hours on Stack Overflow, SitePoint, and all the other websites.
Here is the only code inside my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
Just to make sure it's clear, I want the urls:
website.com/about.php
website.com/home.php
website.com/blog.php
to look like
website.com/about
website.com/home
website.com/blog
It should remove .php from ALL .php files in all directories for my website.
you actually want
RewriteEngine On
#if the requested file doesn't exist on server filesystem
RewriteCond %{REQUEST_FILENAME} !-f
#AND if the file with php extension exists. Just an extra check
RewriteCond %{REQUEST_FILENAME}\.php -f
#AND if the request isn't for domain root
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1\.php?r=1 [L]
#redirect all urls ending with .php to "clean" URL
RewriteCond %{REQUEST_URI} ^/?(.*)\.php$
RewriteCond %{QUERY_STRING} !(^|&)r=1
RewriteRule ^(.*)\.php$ /$1? [L,R=301]
It will not make website.com/about.php look like website.com/about but it will display website.com/about.php if user visits website.com/about.

Do I need to call for .htaccess?

I have no experience with .htaccess, but I got a tip that it's very useful so I wanted to try this.
I now have a file called .htaccess, in my root folder.
The files contains this;
RewriteBase /
RewriteEngine on
RewriteCond %{HTTP_HOST} ^kellyvuijst\.nl [nc]
RewriteRule (.*) http://www.kellyvuijst.nl/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
What I'm trying to do here is create a 'www.mysite.com/portfolio/' instead of 'mysite.com/portfolio.html' I used some tutorials on this and I think it's correct, but I'm not sure.
So now I have this file, and what now? The tutorials all show what to put in the file but not what to do with it? Do I need to call for it in every .html page I have? And how do I call for it?
A .htaccess file is automatically invoked by the server.
You have just to put this into your file :
RewriteBase /
RewriteEngine on
RewriteRule www.mysite.com/portfolio/ /mysite.com/portfolio.html [L]
Hmm, you're using a lot of rules here to achieve just that.
Anyway, no you don't have to include that file. If you're hosting your site on a server with Apache it'll be included automatically. Can you also run PHP files or is your site just HTML? That's always an easy sign if you're also using Apache (not 100%, but often the go together).
If so, you could try just using these rules first:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.+)\.(.+)$ [nc]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
If that always adds www to your address, even if you type in the URL without www at least you can be certain that it works.
Then, to make the .html disappear you can add this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule $(.*)/$ /$1.html [L]
This should make every url that ends with a slash (like portfolio/) use a .html file instead (portfolio.html), but only if /portfolio/ isn't an actual directory on your website.
(I removed your url from the rules because this way it should also work if you use it on another website, or if you change your url. It should still do what you want)
Made sure the server is configured to allow htaccess files to override host options. So in your vhost/server config, you need:
AllowOverride All

merging two folder with mod-rewrite in .htaccess

I'm currently developing a drupal based website but will have to keep a legacy system and many static pages/folders alive. It would be great if i could keep the old rubble in a separate folder than the new site. This would mean I've to merge both folders on the fly. I thought that this folder structure would be great.
/htdocs/legacy (symlink to old web root)
/htdocs/index.php
/htdocs/.htaccess
/htdocs/other drupal files and folders
/htdocs/...
This would mean that if i.e. mydomain.com/xyz.php is accessed the server should try to serve it in the following order.
if file or folder in /htdocs/ server this
if file or folder in /legacy/ serve this but do not rewrite the browsers location bar.
else rewrite pass it as querystring to the
I came up with the following rewrite rules. Which however don't work. I can either serve files in legacy or via drupal.
# 1. server from .htaccess folder
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [NC,L]
# 2. serve from legacy
RewriteCond /full-path-to-legacy-folder/%{REQUEST_URI} -f [OR]
RewriteCond /full-path-to-legacy-folder/%{REQUEST_URI} -d
RewriteRule ^(.*)$ /legacy/$1 [NC,L]
# 3. else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Has anyone of you suggestions?
Many thanks!
Since you're just rewriting the URL, and there are other operations that need to be done with the file (like mod_php), try changing your [L] options to [PT] to allow regular processing to continue on the request.

weird htaccess question

Currently this is my .htaccess
RewriteEngine on
#rewrite the url's
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
So, from the index i render a template, and give it a url.
Normally a page would look like this
www.whatever.com/?url=test/page
But with the rewrite it goes
www.whatever.com/test/page
So the question is {
I have an admin section of the site that I want unaffected by this.
So, /admin needs to access the admin folder in the folder tree.
Thanks for the help
-Wes
The best way to do this is to not re-write the URL's of real files and directories on the filesystem. This can be achieved by adding a couple rewrite conditions to your rule:
RewriteCond %{REQUEST_FILENAME} !-s [OR]
RewriteCond %{REQUEST_FILENAME} !-l [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Now, these mean, respectively, only rewrite urls that are: not a real file (with > 0 size), not a symlink, and not a directory.
Alternatively, you could just make sure your rule does not match your admin directory:
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The first example is by far the most flexible, however, as it won't interfere with any static files, such as images, etc.

Resources