Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have rewrite rules with aliases?

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?

like image 945
david t-g Avatar asked Jun 30 '11 11:06

david t-g


People also ask

Where do you put rewrite rules?

A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.

What is $1 in Apache rewrite rule?

In your rewrite, the ^ signifies the start of the string, the (. *) says to match anything, and the $ signifies the end of the string. So, basically, it's saying grab everything from the start to the end of the string and assign that value to $1.

What is rewrite rule in Apache?

RewriteRule specifies the directive. pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser. substitution is the path to the actual URL, i.e. the path of the file Apache servers. flags are optional parameters that can modify how the rule works.

What is rewrite rule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.


1 Answers

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.

like image 91
Femi Avatar answered Sep 16 '22 18:09

Femi