dongwei3336 2012-02-07 16:44
浏览 22
已采纳

mod_rewrite规则删除comment.php

I need to have

mysite.com/comment.php?id=sasdfkjsfj

redirected to

mysite.com/sasdfkjsfj.

Right now, i'm using this code for doing that

RewriteEngine on
RewriteBase / 

#redirect to remove comment.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /comment\.php\?id=([^\ &]+) [NC]  
RewriteRule ^ %1? [L,R=301] 

#process the SEF Url with comment.php
RewriteRule ^([-a-zA-Z]+)$ comment.php?id=$1 [L]

It changes the url address to

mysite.com/sasdfkjsfj.

but that results a 404 page error. I have a separate comment.php page which is doing the job for

 mysite.com/comment.php?id=sasdfkjsfj

where i get the id by $_GET['id'] from the url but now, how to handle

mysite.com/sasdfkjsfj

after redirection.

And also what changes i have to make for this type of URL

mysite.com/sasdfkjsfj/Url_redirect_option.
  • 写回答

1条回答 默认 最新

  • dtbc37573 2012-02-07 17:06
    关注

    I'm just going to presume you want to redirect mysite.com/sasdfkjsfj to mysite.com/comment.php?id=sasdfkjsfj; the reverese would be unusual.

    Add the following .htaccess into the base web directory, or into your apache config:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    # if you want to use /comment/ as a base dir, as suggested by ThinkingMonkey
    # then you could use this instead
    # RewriteRule ^comment/([a-zA-Z0-9_]+)$ /comment.php?id=$1 [L]
    
    # look for "mysite.com/anyasciichars" without a . or a /
    # the [L] means this is the last rule processed (if it matches)
    RewriteRule ^([a-zA-Z0-9_]+)$ /comment.php?id=$1 [L]
    </IfModule>
    

    To handle the case of mysite.com/sasdfkjsfj/rewrite_rule, do something like this:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    # look for "mysite.com/anyasciichars/rewrite_rule"
    # the [L] means this is the last rule processed (if it matches)
    RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$ /comment.php?id=$1&rule=$2 [L]
    </IfModule>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?