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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵