mod_rewrite规则删除comment.php

发布于 2025-01-03 12:18:30 字数 840 浏览 3 评论 0原文

我需要

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2025-01-10 12:18:30

我假设您想将 mysite.com/sasdfkjsfj 重定向到 mysite.com/comment.php?id=sasdfkjsfj;相反的情况会很不寻常。

将以下 .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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文