Htaccess RewriteEngine
I have a url which is http://www.urlbookmarking.com/bookmarks-details.php?bid=55 and I want it to be similar to http://www.urlbookmarking.com/bookmark/55
I wrote in htaccess:
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
But when I go to the first URL, the rewrite engine doesn't apply my rule. Is there any bug or conflict somewhere?
My complete htaccess file written as follows
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
Please help me.
source to share
The line Options +FollowSymLinks
is optional if it is already configured in httpd.conf
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking\.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [R=301, L]
RewriteRule ^bookmark/([0-9]+)$ bookmarks-details.php?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
source to share
A few things:
-
RewriteEngine On
only needs to be called once, although this may not cause any problems. - I also have
RewriteBase /
after my lineRewriteEngine On
- My rewrite rule looks like this
RewriteRule ^common/(.*)$ common.php?file=$1 [QSA,L]
, which tells me your rule should look like this:RewriteRule ^bookmark/(.*) bookmarks-details.php?bid=$1 [QSA,L]
source to share
Do you want your url to be /bid/55
or /bookmark/55
? because you wrote it like it would be /bid/55
...
Anyway, your .htaccess should look something like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^bid/(.*)$ bookmarks-details.php?bid=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
... without multiple directives RewriteEngine on
(they don't break anything, but they are not necessary) and without a leading slash in the rewrite rule bid
. Also, put your new rule in front of the rules that overwrite for a non-existent file so it doesn't rewrite your url before you can use it, and add a flag [L]
to the rule so it doesn't get further modified by other rules. Also, add a start / end line marker ( ^
/ $
) to the rule.
If you were typing rules in httpd.conf
, you would only use the following slash, you don't use them in files .htaccess
.
If you want your urls to be /bookmark/
, just replace bid
with bookmark
.
source to share
This should redirect all '/bookmarks-details.php\?bid=(id)' URLs with bookmark ids (which only have numbers) to / bookmark / (id).
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
Once you have successfully rewritten the URL, you need to write a companion rule to handle it, for example:
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
If should go between the rule that always adds "www" at the beginning and the catch rule that I put at the end. Together, it might look like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This link can make it clearer: http://corz.org/serv/tricks/htaccess2.php
source to share