merging two folder with mod-rewrite in .htaccess - .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.

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]

.htaccess. Redirect requests to ~username folder

Situation:
I'm moving a website from a production environment to a test environment.
The test environment url is similar to http://192.168.1.100/~username/
There are thousands of files which use the following within the html
<img src='/images/image.jpg' />
Since the request is going to root http://192.168.1.100/ the files are 404.
Rather than finding and replacing all of html I'd assume that there is an easy way to fix it with mod_rewrite via .htaccess.
I've tried using the following
RewriteCond %{REQUEST_URI} !^/~username/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~username/$1
But did not work as expected.
Thanks in advance.
UPDATE
The development environment resides within cpanel/whm. So when the username is removed from the requested url, it now belongs to the root users. So, my question now: How do I update the .htaccess file for the root user to mod_rewrite back to the ~username?
If you remove
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
it appears to work as expected, because any request to the right url will not be rewritten.
you might want to add [L] as a flag to signify it's the last rewrite rule, like so:
RewriteCond %{REQUEST_URI} !^/~username/
RewriteRule ^(.*)$ /~username/$1 [L]

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

.htaccess URL Rewriting for Folders

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.

Using mod_rewrite only if a 404 would have occured

I am in the process of converting a static website into one using a cms.
I have the cms installed in a sub directory of the public directory. To avoid ending up with ugly domain names (http://example.com/cms/) is there an easy way using mod_rewrite to rewrite http://example.com/… to http://example.com/cms/… while ensuring that if the request wouldn't have ended in a 404, there is no redirect.
An example:
/
/cms/index.html
/cms/file.dat
/file.dat
If the user requests /index.html, they should get redirected to /cms/index.html, but if they request /file.dat, they shouldn't get redirected to /cms/file.dat because the file existed at the requested place
EDIT
Thanks for the answers.
You could use the RewriteCond Directive to check whether there is an existing file that correspond to the requested URL, and only rewrite to your CMS if there is none.
Here is a simple example :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
If there is no existing file that correspond to the requested URL, then that request is rewritten to index.php
You might also want to check for symbolic links and / or directories, btw...
For instance, here is a possibility that can be used when setting up a Zend Framework project :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
(Even though it links to ZF, it should be OK for quite many projects)
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^cms/ cms%{REQUEST_URI} [L]

Resources