简体   繁体   中英

htaccess RewriteEngine

I have an url which is http://www.urlbookmarking.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:

  1. RewriteEngine On only needs to called once, though this may not be causing any problems
  2. I also have RewriteBase / after my RewriteEngine On line
  3. My rewrite rule looks like this: RewriteRule ^common/(.*)$ common.php?file=$1 [QSA,L] , which tells me that your rule should looke like this RewriteRule ^bookmark/(.*) bookmarks-details.php?bid=$1 [QSA,L]
  1. you should use only one RewriteEngine on
  2. RewriteRule /bid/(.*) bookmarks-details.php?bid=$1 - put this line after Options +FollowSymLinks
  3. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM