WAMP + RewriteRule : Copying site to subfolder - apache

I've just installed WAMP, and now I'm trying to copy my files from my server to my local machine. I have a rewrite rule like
RewriteRule .* /index.php?url=$0 [L,QSA]
But that seems to redirect to http://localhost/index.php when I actually want http://localhost/mysite/index.php. So I figured
RewriteBase /mysite
Would do the trick, but that doesn't appear to be the case. Doesn't seem to work that way from my readings. Is there any way that I can fix this without changing my rewrite rules? I basically just want the base of my site to be in some subfolder.

I got this working by having a trailing slash on the RewriteBase string:
RewriteBase /aliasdirectory/

if you’re using relative paths in your substitution, you don’t need to alter the base URI path:
RewriteRule .* index.php?url=$0 [L,QSA]

Related

Apache URL Rewriting for multiple dynamic folder levels

Wondering if somebody can help me write some RewriteRule's for my website.
Take a look at the following URLs and see how I need to rewrite them.
http://www.example.com/essays-and-reports/
does not need to be re-written, it is a physical folder on the web server.
http://www.example.com/essays-and-reports/business/
needs to rewrite to (root)/first_level_template.php
http://www.example.com/essays-and-reports/dynamic_name2
needs to rewrite to (root)/first_level_template.php
http://www.example.com/essays-and-reports/business/financial-reports/
needs to rewrite to (root)/second_level_template.php
http://www.example.com/essays-and-reports/blah/financial-reports/C_B_2413_Report_on_savings.php
needs to rewrite to (root)/final_level_template.php
Note the rules must work regardless of a trailing slash. To sum-up, there are three levels which I need to re-write to their relevant template. None of above exists physically on the server including the PHP file for final level. The only thing that exists is the essays-and-reports folder which is main folder for the website.
I tried something like this but I get compile errors in the log.
RewriteRule ^([^/]+)/([^/]+)/?$ /second-level-template\.php [L]
If you could help me write the rules I need - I appreciate it greatly.
EDIT:
This code kind of works but it also rewrites the essays-and-reports folder which I don't want...
Options +FollowSymLinks
RewriteEngine On
RewriteBase /essays-and-dissertations/
RewriteRule ^[^/]+/[^/]+/[^/]+/?$ /final_level_template.php [L]
RewriteRule ^[^/]+/[^/]+/?$ /second_level_template.php [L]
RewriteRule ^[^/]+/?$ /first_level_template.php [L]
You could do this with 3 rules:
RewriteRule ^essays-and-reports/[^/]+/?$ /first_level_template.php [L]
RewriteRule ^essays-and-reports/[^/]+/[^/]+/?$ /second_level_template.php [L]
RewriteRule ^essays-and-reports/[^/]+/[^/]+/[^/]+/?$ /final_level_template.php [L]
You don't need to escape the dot in your redirect target, since that's not a regular expression.

How to do a mod_rewrite redirection to relative URL

I am trying to achieve a basic URL redirection for pretty-URLs, and due to images, CSS etc. also residing in the same path I need to make sure that if the URL is accessed without a trailing slash, it is added automatically.
This works fine if I put the absolute URL like this:
RewriteRule ^myParentDir/([A-Z0-9_-]+)$ http://www.mydomain.com/myParentDir/$1/ [R,nc,L]
But if I change this to a relative URL, so that I don't have to change it each time I move things in folders, this simply doesn't work.
These are what I tried and all do not work, or redirect me to the actual internal directory path of the server like /public_html/... :
RewriteRule ^myParentDir/([A-Z0-9_-]+)$ ./myParentDir/$1/ [R,nc,L]
RewriteRule ^myParentDir/([A-Z0-9_-]+)$ myParentDir/$1/ [R,nc,L]
What is the right way to do a URL redirection so that if the user enters something like:
http://www.mydomain.com/somedir/myVirtualParentDir/myVirtualSubdir
he gets redirected to (via HTTP 301 or 302):
http://www.mydomain.com/somedir/myVirtualParentDir/myVirtualSubdir/
Thanks.
EDIT: Adding some more details because it does not seem to be clear.
Lets say I am implementing a gallery, and I want to have pretty URLs using mod_rewrite.
So, I would like to have URLs as follows:
http://www.mydomain.com/somedir/galleries/cats
which shows thumbnails of cats, while:
http://www.mydomain.com/somedir/galleries/cats/persian
which shows one image from the thumbnails of all cats, named persian.
So in actual fact the physical directory structure and rewriting would be as follows:
http://www.domain.com/somedir/gallery.php?category=cats&image=persian
So what I want to do is put a .htaccess file in /somedir which catches all requests made to /galleries and depending on the virtual subdirectories following it, use them as placeholders in the rewriting, with 2 rewrite rules:
RewriteRule ^galleries/(A-Z0-9_-]+)/$ ./gallery.php?category=$1 [nc]
RewriteRule ^galleries/(A-Z0-9_-]+)/+([A-Z0-9_-]+)$ ./gallery.php?category=$1&image=$2 [nc]
Now the problem is that the gallery script in fact needs some CSS, Javascript and Images, located at http://www.domain.com/somedir/css, http://www.domain.com/somedir/js, and http://www.domain.com/somedir/images respectively.
I don't want to hardcode any absolute URLs, so the CSS, JS and Images will be referred to using relative URLs, (./css, ./js, ./images etc.). So I can do rewriting URLs as follows:
RewriteRule ^galleries/[A-Z0-9_-]+/css/(.*)$ ./css/$1 [nc]
The problem is that since http://www.domain.com/somedir/galleries/cats is a virtual directory, the above only works if the user types:
http://www.domain.com/somedir/gallaries/cats/
If the user omits the trailing slash mod_dir will not add it because in actual fact this directory does not actually exist.
If I put a redirect rewrite with the absolute URL it works:
RewriteRule ^galleries/([A-Z0-9_-]+)$ http://www.mydomain.com/subdir/galleries/$1/ [R,nc,L]
But I don't want to have the URL prefix hardcoded because I want to be able to put this on whatever domain I want in whatever subdir I want, so I tried this:
RewriteRule ^galleries/([A-Z0-9_-]+)$ galleries/$1/ [R,nc,L]
But instead it redirects to:
http://www.mydomain.com/home/myaccount/public_html/subdir/galleries/theRest
which obviously is not what I want.
EDIT: Further clarifications
The solution I am looking for is to avoid hardcoding the domain name or folder paths in .htaccess. I am looking for a solution where if I package the .htaccess with the rest of the scripts and resources, wherever the user unzips it on his web server it works out of the box. All works like that apart from this trailing slash issue.
So any solution which involves hardcoding the parent directory or the webserver's path in .htaccess in any way is not what I am looking for.
Here's a solution straight from the Apache Documentation (under "Trailing Slash Problem"):
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]
Here's a solution that tests the REQUEST_URI for a trailing slash, then adds it:
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.+) http://www.example.com/$1/ [R=301,L]
Here's another solution that allows you to exempt certain REQUEST_URI patterns:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
Hope these help. :)
This rule should add a trailing slash to any URL which is not a real file/directory (which is, I believe, what you need since Apache usually does the redirect automatically for existing directories).
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ $1/ [L,R=301]
Edit:
In order to prevent Apache from appending the path relative to the document root, you have to use RewriteBase. So, for instance, in the folder meant to be your application's root, you add the following, which overrides the physical path:
RewriteBase /
This might work:
RewriteRule ^myParentDir/[A-Z0-9_-]+$ %{REQUEST_URI}/ [NS,L,R=301]
However, I'm not sure why you think you need this at all. Just make your CSS / JS / image file rewrite rule look something like this:
RewriteRule ^galleries/([A-Za-z0-9_-]+/)*(css|js|images)/(.*)$ ./$2/$3
and everything should work just fine regardless of whether the browser requests /somedir/galleries/css/whatever.css or /somedir/galleries/cats/css/whatever.css or even /somedir/galleries/cats/persian/calico/css/whatever.css.
Ps. One problem with this rule is that it prevents you from having any galleries names "css", "js" or "images". You might want to fix that by naming those virtual directories something like ".css", ".js" and ".images", or using some other naming scheme that doesn't conflict with valid gallery names.
I'm not sure I complelty understand your problem.
The trailing slash redirection is done automatically on most Apache installation because of mod_dir module (99% of chance you'have the mod_dir module).
You may need to add:
DirectorySlash On
But it's the default value.
So. If you access foo/bar and bar is not a file in foo directory but a subdirectory then mod_dir performs the redirection to foo/bar/.
The only thing I known that could break this is the Option Multiviews which is maybe trying to fin a bar.php, bar.php, bar.a-mime-extension-knwon-by-apache in the directory. So you could try to add:
Option -Multiviews
And remove all rewriteRules. If you do not get this default Apache behavior you'll maybe have to look at mod-rewrite, but it's like using a nuclear bomb to kill a spider. Nuclear bombs may get quite touchy to use well.
EDIT:
For the trailing slash problem with mod-rewrite you can check this documentation howto, stating this should work:
RewriteEngine on
RewriteBase /myParentDir/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]

htaccess rewrite of directory to variable fails on localhost

Let's take something like this question. So, I'm looking to rewrite:
http://www.example.com/test to
http://www.example.com/page.php?v=test
with:
RewriteRule ^(.*)$ page.php?v=$1 [L]
This takes me to the page http://www.example.com/test/?v=test
Why does it not stay on http://www.example.com/test without the trailing slash and the query string.
PS: I'm using WampServer on Windows
in RewriteRule don't put / and the begining!
RewriteRule ^(.*)$ page.php?id=$1 [L]
The problem arises if test is an existing directory on the server. Apache then uses the DirectorySlash Directive to "fix up" the URL pointing to the directory by adding a trailing slash.
I'm answering my own question here, but the solution (as the above link states) is to add DirectorySlash Off to .htaccess

Apache .htaccess Rewrite Query String with slashes

I am trying to figure out Apache's Mod Rewrite and so far it's not working. Here is what I am trying to do.
I have an index.php in the root directory of my site that is a template for all pages. I want to be able to organize my files in directories and the query string will have slashes.
So...
http://www.example.com/page.html
should be...
http://www.example.com/index.php?url=page
and...
http://www.example.com/directory/page.html
should be...
http://www.example.com/index.php?url=directory/page
I've got that working but I want to be able to go n directories deep...
http://www.example.com/directory1/directory2/page.html
should be...
http://www.example.com/index.php?url=directory1/directory2/page
I know I could just put a bunch of rewrites for however many directories I deep I want to go, but is there a single line RewriteRule that will put anything after the first / will be considered a query string?
This is what I have so far...
RewriteEngine on
RewriteRule ^([a-z-]+)\.html$ index.php?url=$1
RewriteRule ^faculty/([a-z-]+)\.html$ index.php?url=faculty/$1
Behind the scenes, PHP is including the file that is in the location that the url is specified.
This is bad idea to include file which name was passed from user. At least implement very STRONG validation, otherwise prepare to be hacked.
If you still want to use this -- here is the rule:
RewriteRule ^(.+)\.html$ index.php?url=$1 [QSA,L]
this should redirect anything with a ending .html to index.php?url=anything
www.example.com => index.php?url=
www.example.com/test1/test2 => index.php?url=test1/test2
RewriteRule ^([a-z-]+)\.html$ index.php?url=$1 [L,NC]

Apache Redirect does not work…

Once in a while I have to fuddle around with mod_rewrite or rather Apache's redirect. Now I have a simple task, that drives me nuts, cause I can't get it working:
Everything that was under /journal/(.*) is now under /blog/(.*).
This is what I have now:
RewriteEngine on
RewriteBase /
RewriteRule ^/journal/([^/]+)$ /blog/$1
If it matters: this is a drupal installation.
Drupal has existing rewrite rules in .htaccess. If you put your rules at the top of the file, Drupal's rules may override them.
I don't think you want the starting / in your rule.
If you're expecting the rule to redirect folks who use the old /blog/ URL, you're mistaken. If that's what you're trying to do, you'll need to perform a redirection, like this:
.
RewriteEngine on
RewriteBase /
RewriteRule ^journal/(.+)$ http://example.com/blog/$1 [R=301,L]
since your rewrite base is / I think you can leave out the / in front of journal. so try
RewriteRule ^journal/([^/]+)$ /blog/$1
Also if you have any other rules happening you can append [L] to the end of the statement to make sure no other rewrites happen.