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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题