omniuni wrote:Hello, all.
So, the regex I had, told mod_rewrite to extract the stuff after / and put it after ?page=
In otherwords:
http://example.com/contact would be translated to
http://example.com/?page=contact
If someone could help me re-create this single mod-rewrite rule, I would be very grateful.
The re-written target URL should specify the name of a script file that will handle the query parameters - e.g.
index.php?page=contact. Assuming your script file is index.php in the root directory, try this:
Code: Select all
RewriteEngine on
# if the requested URL is not a real file...
# and the requested URL is not a real directory...
# then capture the URL and rewrite it.
# Note: URL must be exactly one path level deep
# Note: URL may have optional trailing / which is stripped off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1
Yes and no. You cannot write one rule to handle arbitrary depths (passing each directory in a separate query variable), but you can write one rule for each path depth that needs to be handled. (URLs having longer paths will get an Apache
"Not Found" page.) The following .htaccess handles three levels deep:
Code: Select all
RewriteEngine on
# one level deep
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?var1=$1
# two levels deep
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?var1=$1&var2=$2
# three levels deep
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?var1=$1&var2=$2&var3=$3
Alternatively, you could capture the whole path string (having arbitrary depth) and re-write it into one query variable, and then parse the string in your script file. Here is a .htaccess file that does this:
Code: Select all
RewriteEngine on
# pass multi-level path in one query variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ index.php?multipath=$1
omniuni wrote:Darn it, I really need to learn regex.

Yes, but beware... they can become addicting!
Hope this helps
