Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference the current directory from .htaccess using mod_rewrite?

Tags:

mod-rewrite

I'd like to use mod_rewrite to make pretty URLs, but have a single version of the .htaccess file that can be used for any user on a server.

So far I have the standard pretty URL .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$     /index.php/$1 [L]

Ideally, I would like something similar to

RewriteRule ^(.*)$     %{URL of this file's directory}/index.php/$1 [L]

This documentation would lead me to believe that what I want is not necessary:

Note: Pattern matching in per-directory context Never forget that Pattern is applied to a complete URL in per-server configuration files. However, in per-directory configuration files, the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done. This feature is essential for many sorts of rewriting - without this, you would always have to match the parent directory which is not always possible.

I still get the wrong result, though, whether or not I put a leading / in front of the index.php on the RewriteRule.

This,

http://server/test/stream/stream

turns into

http://server/index.php/stream

not

http://server/test/index.php/stream

when the .htaccess file is in /test/.

like image 914
Allan Avatar asked Jun 12 '09 00:06

Allan


People also ask

How to exclude folder from rewrite rule in htaccess?

Here are the steps to exclude folder from rewrite rule in .htaccess. 1. Enable mod_rewrite If you have already enabled mod_rewrite in your Apache server, you can skip this step. Otherwise run the following commands to enable mod_rewrite, depending on your Linux Open Apache configuration file in a text editor.

How do I use rewriterule in a htaccess file?

When using RewriteRule in .htaccess files, be aware that the per-directory context changes things a bit. In particular, rules are taken to be relative to the current directory, rather than being the original requested URI. Consider the following examples:

Can a htaccess file in a particular directory override directives?

Therefore, a .htaccess file in a particular directory may override directives found in .htaccess files found higher up in the directory tree. And those, in turn, may have overridden directives found yet higher up, or in the main server configuration file itself.

How do I change the name of a directory with htaccess?

A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof. If you want to call your .htaccess file something else, you can change the name of the file using the AccessFileName directive.


2 Answers

I was having a similar problem, but found that simply leaving off the root '/' resulted in my test web server (xampp on windows) serving a URL similar to:

http://localhost/C:/xampp/htdocs/sites/test/

Not perfect. My solution is to use a RewriteCond to extract the path from REQUEST_URI. Here's an example for removing the unwanted "index.php"s from the end of the URL:

RewriteCond %{REQUEST_URI} ^(.*)/index.php$
RewriteRule index.php %1/ [r=301,L]

Note: the percent sign in "%1/", rather than a dollar sign.

%1-%9 gets the patterns from the last matched RewriteCond, $1-$9 gets the patterns from the RewriteRule.

See: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond

Cheers :)

like image 190
Li1t Avatar answered Oct 15 '22 14:10

Li1t


Turns out that it does matter whether I put a leading / in front of index.php or not. By leaving it off, the script works correctly. I had been testing with the R flag which was using physical directories on the redirect.

like image 24
Allan Avatar answered Oct 15 '22 14:10

Allan