How can I have rewrite rules with aliases? - apache

I apologize in advance if this is too long. I figure more detail is better than less and hope I'm not being horribly rambling :-)
I use WAMP on my laptop for local dev, and I have various c:/wamp/alias/* files each pointing to a project working directory. I've had some excellent mod_rewrite help and gotten
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*\/)?ih(\/.*)?$ $1index.php$2 [L,QSA]
to work to change localhost/.../ih/sub/dir to localhost/.../index.php/sub/dir for URLs that are both SEO-friendly and short. [I haven't gone on to do this in prod but I suspect it will work just as well.] However, to get it all together I had to change my doc root from c:/wamp/www/ to c:/, which I'd really rather not do just in case my Apache gets hacked and otherwise because it's a kludge.
My test alias file looks like
Alias /testme "c:/var/tmp/wamp-testme/"
<Directory "c:/var/tmp/wamp-testme/">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*\/)?ih(\/.*)?$ $1index.php$2 [L,QSA]
</Directory>
and the error I get when trying to load http://localhost/testme/rewrites/ih/sub/path is
[Thu Jun 30 06:46:13 2011] [error] [client 127.0.0.1]
File does not exist: C:/wamp/www/var
with a matching
Not Found
The requested URL /var/tmp/wamp-testme/rewrites/index.php/sub/path
was not found on this server.
in the browser. Sure enough, the same config in my c:/wamp/alias/flying.conf file that points to the c:/data/flying/ directory throws File does not exist: C:/wamp/www/data in the error log file, and so on.
Sooooo... How can I have a rewrite rule that transcends aliases without having my doc root at my machine root dir?

Odd: try adding a PT flag to the rewrite rule: this forces the re-written URL to be sent back to the URL mapping engine.
EDIT: try setting the RewriteBase on the Directory.

Related

Rewriting URL over a file .htaccess

Here is my problem.
I know how to rewrite a URL only if the file doesn't exist.
But I came across a problem that I have never encountered before.
Given the URL : http://www.my-host.com/agences/my-agencies
With at the directory root 2 files :
agences.php
.htaccess
In the .htaccess :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^agences/(.*) /agences.php?agence=$1
This does not redirect to the /agences.php and is not even interpreted.
If I change the RewriteRule by:
RewriteRule ^agences/(.*) $1
It doesn't even process the rewrite rules.
And so even if I prepend the slash to the regex condition like this :
RewriteRule ^/agences/(.*) $1
I run on an apache 2.4.10, with the AllowOverride all configure in the vhost.
Thanks for the help.
Add that at the beginning of the code:
Options -MultiViews
The effect of MultiViews is as follows: if the server receives a
request for /some/dir/foo, if /some/dir has MultiViews enabled, and
/some/dir/foo does not exist, then the server reads the directory
looking for files named foo.*, and effectively fakes up a type map
which names all those files, assigning them the same media types and
content-encodings it would have if the client had asked for one of
them by name. It then chooses the best match to the client's
requirements. http://httpd.apache.org/docs/2.0/en/content-negotiation.html

enabling RewriteEngine in apache (uniserver) causes 403 on all files

What I'm trying to do is to enable a 'dry' version of url rewriting in Apache only to check if it is working and actually do not rewrite anything yet.
The simplest .htaccess that I came up with is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
But this still doesn't work. All files, all paths I'm trying to request, no matter if they exist or not return 403 Forbidden
What I need is any example htaccess that has redirection inside that must work, that is allow access to all existing files and folders and do it's job only when the file requested doesn't exit.
The problem was that I've not added these lines:
Options +SymLinksIfOwnerMatch
or
Options +FollowSymlinks

htaccess rewrite - 404 on existing file

So I have the following rewrite in an htaccess file:
RewriteRule ^([^/.]+)/?$ index.php?page=$1 [L]
Whenever I try to go to http://domain/index I get a 404, but when I try to go to http://domain/index. or even a non-existent page like http://domain/a, the rewrite works just fine and index.php var_dump()s the appropriate values.
The only code in index.php is var_dump($_GET);, so I know it's not a php issue.
Can someone explain to me what's wrong with my rewrite rule, and explain how to fix it?
EDIT:
I forgot I had error logging enabled. The error it keeps saving to error.log is:
[Sun Feb 24 21:01:18 2013] [error] [client 192.168.1.1] Negotiation: discovered file(s) matching request: /path/public_html/index (None could be negotiated).
By the error, it seems MultiViews is enabled.
You may try this at the top of the file:
Options +FollowSymlinks -MultiViews
Additionally, I suggest the following:
# Prevent loops
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/? index.php?page=$1 [L]
MultiViews provides for Content Negotiation, best described here
When MultiViews is enabled (Options +MultiViews), the Content Negotiation module looks for files that match index, but .php files don't qualify for a match, so it fails with None could be negotiated. You can add .php handlers as matches using:
MultiviewsMatch Handlers Filters
as documented in MultiviewsMatch.

Apache htaccess file, where did I go wrong?

Really can't find my mistake, I can access the sitemap/ link, but all the others give a 404 Not Found Error.
DirectoryIndex index.php index.php?page=home index.php?page=error
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
# some other stuff #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule ^sitemap/?$ sitemap.xml.php [L]
RewriteRule ^([a-zA-Z]+)/?$ index.php?page=$1 [L]
RewriteRule ^products/?$ index.php?page=products [L]
RewriteRule ^products/([0-9]+)/?$ index.php?page=products&id=$1 [L]
</IfModule>
I'd really appreciate some help, I definitely suck at .htaccess files...
Rewriting rules aside, you have other problems here:
DirectoryIndex index.php index.php?page=home index.php?page=error
This means, that, for a folder a file showing index will be selected in this order (first one existing wins):
index.php
index.php?page=home (this is the filename)
index.php?page=error (this is the filename)
I doubt this was your intention.
Next:
AllowOverride All
If you're already in .htaccess and AllowOverride was restrictive, this will not help, and if it wasn't, then there is no point of writing this again.
Order allow,deny
Allow from all
This basically says "allow everyone everywhere" which is the default anyway. Unless it was restricted in some way on upper level or in apache configuration, this is redundant.
As for rewrite:
RewriteRule ^([a-zA-Z]+)/?$ index.php?page=$1 [L]
RewriteRule ^products/?$ index.php?page=products [L]
The second rule is redundant anyway, and will never be reached (as the first one matches it as well).
As for the error you're seeing:
[Wed Jul 04 02:56:30 2012] [error] [client 127.0.0.1] File does not exist: /var/www/home"
This may mean problems with DocumentRoot definitions. Please, post your VirtualHosts configuration.

.htaccess RewriteRule adds drive path to URL

I am using Zend Server CE (v.5.1.0) installed on C: on a Win7 machine. I have added one project to httpd.conf by adding:
Alias /project "D:\Homepages\project"
<Directory "D:\Homepages\project">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
My .htaccess file in the project directory contains the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/\w*\.(css|js) [NC]
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Now to the problem; if I go to
http://localhost/project/index.php
everything seems to be working fine. I reach the index.php file and get my contents.
However, if I go to any other page that would trigger the RewriteRule, it seems to be adding the directory path. FireFox outputs the following Not Found message:
The requested URL /Homepages/project/index.php was not found on this server.
I tried to find a similar question/answer here, but failed. Any idea?
Ps. Me accepting of an answer might be delayed as I will be out for a while on an errand.
You need to set the RewriteBase directive; otherwise, mod_rewrite automatically, and by default, prepends the file path to the resulting rewrite rule.
From: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. However, for most websites, URLs are NOT directly related to physical filename paths, so this assumption will often be wrong! Therefore, you can use the RewriteBase directive to specify the correct URL-prefix.
If your webserver's URLs are not directly related to physical file paths, you will need to use RewriteBase in every .htaccess file where you want to use RewriteRule directives.
Have your last line like this:
RewriteRule ^.*$ /index.php [NC,L]
However I think this is infinite loop so I would suggest this rule instead:
RewriteCond %{THE_REQUEST} !\s/index.php [NC]
RewriteCond %{REQUEST_URI} !^/index.php [NC]
RewriteRule . /index.php [L]
which prevents going to index.php if it is already /index.php.