Hello guys, well, I’ve been searching everywhere and still can’t figure out why my mod_rewrite code doesn’t work.
I have links like ?id=something and want to have /something, so that’s nothing special at all. I’ve checked my scripts on two different servers (latest XAMPP locally and my company’s server, where mod_rewrite is ON.
The file is named .htaccess and I’m putting it right into site’s directory.
So what’s wrong?
RewriteEngine On
RewriteRule ^id/([^/\\.]+)/?$ index.php?id=browse
A screenshot can be found here.
PS. Can I make clickable miniature of the image above using only BB code on sitepoint’s forums? Because it may be too wide for some of you guys, sorry in advance.
Thanks a lot.
matt,
I believe you’re nearly there - don’t give up!
First, you create the link the way that you want to display it to the visitor (that means that the trailing / is your option - and I’d recommend removing that as it WILL cause directory level problems).
Second, your Apache core directives should already be taken care of in the httpd.conf. If not, they’re fine as they are (I’ll leave them in the following code).
PLEASE DON’T quote code - use the code tag so it’s retained in a reply!
Finally, use
Options +FollowSymLinks -MultiViews
# +FollowSymLinks is almost always in the httpd.conf - it's part of enabling mod_rewrite
# -MultiViews prevents using a filename in the path
RewriteEngine On
# ensures mod_rewrite is not in the comment mode
RewriteRule ^id/([0-9]+)$ index.php?id=$1 [QSA,L]
# for Apache 2.x, will match a URI starting with id/
# followed by (and capturing) one or more digits then
# redirect to index.php with an id value of the digits captured
# the QSA flag will retain any existing query string and
# the Last flag will terminate this mod_rewrite block statement
NOTA BENE #1: Your URI MUST match this format for the match and redirection, i.e., id/123 will but 123 will NOT.
NOTA BENE #2: You will HAVE to tell the index.php script that it is located in the DocumentRoot OR use absolute URLs for its css, js and image files (relative links will be referenced to the non-existent id subdirectory - more in my signature’s tutorial on this).
Regards,
DK
Thanks, but I don’t get it at all, URL’s are getting user-friendly, but the whole syntax of back end, regex is webdev-killing.
So I’ve already:
?id=browse
?id=add
?id=something
?id=(…)
And I want to have:
/browse/
/add/
/something/
/(…)/
I’ve tried every tutorial over there, I believe my problem is rather usual, I’ve found this solution, but it doesn’t work, still:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^id/([0-9]+)/?$ /index.php?id=$1 [QSA,L]
This one also:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^id/([^/]+)$ /index.php?id=$1 [QSA,L]

matt,
RewriteEngine On
RewriteRule ^id/([^/\\.]+)/?$ index.php?id=browse
That says to capture one or more of everything after id/ (on an Apache 2.x server) which is NOT / or \ or dot - you do NOT escape the dot character within a range definition) which may have a trailing / (BAD idea) and redirect to index.php with a query string if id=browse. A fixed value for id is not what you’re trying to do, use $1 - you captured it for a reason!
If you need more information about this, the tutorial linked in my signature (and much of it is in this board’s sticky post) has more information about regex and using captured values in the redirection - with sample code!
Regards,
DK