Help with mod_rewrite rule for dynamic url

Uh .. mod_rewrite makes me feel stupid. I just haven't wound my brain yet.: /

I have this url:

http://example.com/a/name/

... what I want to point out here:

http://example.com/a/index.php?id=name

... where name

is what is passed in index.php

as an argument id

.

Everything I've tried results in either 404 or 500 .. :(

+1


a source to share


4 answers


To turn off:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?a/([^/]+)/?$ /a/index.php?id=$1 [QSA,L]

      



If rewriting the tutorial doesn't work for you, try another one .

Edit: Deleted index.php

as suggested by Gumbo

+1


a source


If you want the trailing slash optional, you must exclude the file for which you are rewriting the request. Otherwise, you have a nice infinite recursion.

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/a/index\.php$
RewriteRule ^/a/([^/]+)/?$ /a/index.php?id=$1 [L]

      

Here any query starting with /a/…

, but not /a/index.php

, rewritten to /a/index.php

.



But if the trailing slash is required, there is no need to exclude the target file:

RewriteEngine on
RewriteRule ^/a/([^/]+)/$ /a/index.php?id=$1 [L]

      

+2


a source


Perhaps something similar to strings




RewriteEngine on
RewriteBase /a/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L,QSA]

      



would do the trick.

+1


a source


I suggest you take a look at this url:

http://www.dracos.co.uk/code/apache-rewrite-problem/

The solutions presented will work, but there are explanations in the url, mainly regarding? and # in the URLs themselves.

+1


a source







All Articles