【问题标题】:mod_rewrite rule to remove comment.phpmod_rewrite 规则删除comment.php
【发布时间】:2020-01-16 19:28:56
【问题描述】:

我需要

mysite.com/comment.php?id=sasdfkjsfj

重定向到

mysite.com/sasdfkjsfj.

现在,我正在使用此代码来执行此操作

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]

它将url地址更改为

mysite.com/sasdfkjsfj.

但这会导致 404 页面错误。我有一个单独的 comment.php 页面,它正在为

做这项工作
 mysite.com/comment.php?id=sasdfkjsfj

我通过 $_GET['id'] 从 url 获取 id 但现在,如何处理

mysite.com/sasdfkjsfj

重定向后。

还有我必须对这种类型的 URL 进行哪些更改

mysite.com/sasdfkjsfj/Url_redirect_option.

【问题讨论】:

  • 您是否通过index.php处理您的cmets?
  • @ThinkingMonkey 不,我有一个单独的页面comment.php。我想要的是在重定向之后如何显示之前由 mysite.com/comment.php?id=sasdfkjsfj 显示的内容

标签: php apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

我只是假设你想将mysite.com/sasdfkjsfj 重定向到mysite.com/comment.php?id=sasdfkjsfj; reverese 是不寻常的。

将以下 .htaccess 添加到基本 Web 目录或您的 apache 配置中:

<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>

要处理mysite.com/sasdfkjsfj/rewrite_rule 的情况,请执行以下操作:

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2018-02-03
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2020-04-18
    相关资源
    最近更新 更多