htaccess RewriteEngine
I have an url which is http://www.urlb开发者_StackOverflow中文版ookmarking.com/bookmarks-details.php?bid=55 and I want it to be like http://www.urlbookmarking.com/bookmark/55
I wrote in my htaccess:
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
But when I go to the first URL the rewrite engine does not apply my rule. Is there any mistake, or conflict somewhere?
My full 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.
The line Options +FollowSymLinks
is optional if 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]
A few things:
RewriteEngine On
only needs to called once, though this may not be causing any problems- I also have
RewriteBase /
after myRewriteEngine On
line - My rewrite rule looks like this:
RewriteRule ^common/(.*)$ common.php?file=$1 [QSA,L]
, which tells me that your rule should looke like thisRewriteRule ^bookmark/(.*) bookmarks-details.php?bid=$1 [QSA,L]
- you should use only one
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
- put this line afterOptions +FollowSymLinks
- Try again
Do you want you url to be /bid/55
or /bookmark/55
? because you have written it as if it is going to be /bid/55
...
Anyway, your .htaccess should look more 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 the multiple RewriteEngine on
directives (these don't break anything but are unnecessary), and without the leading forward slash on the bid
rewrite rule. Also, put your new rule before the rules that rewrites for non-existent file so it doesn't rewrite your URL before you get a chance to use it, and add a [L]
flag to the rule so it doesn't get further modified by the other rules. Also, add the line start/end markers (^
/$
) to the rule.
You would only use the leading forward slash if you were putting the rules in httpd.conf
, you don't use them in .htaccess
files.
If you want your urls to be /bookmark/
, just replace bid
with bookmark
.
This should redirect all '/bookmarks-details.php\?bid=(id)' urls with bookmarks ids (that have only numbers) to /bookmark/(id).
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
Once you successfully rewritten the URL, you then need to write a companion rule to process it, like so:
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
If should go between the rule that always adds 'www' to the beginning and the catch all rule, which I placed at the end. All together, it may look like so:
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 may make things clearer: http://corz.org/serv/tricks/htaccess2.php
精彩评论