如何使用.htaccess重写URL(删除ID?= 1) [英] How to rewrite URLs with .htaccess (removing id?=1)

查看:63
本文介绍了如何使用.htaccess重写URL(删除ID?= 1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看了很多遍,还没有弄清楚如何进行这项工作,我相信对于知道他们在做什么的人来说,这非常简单.首先,我已经确保启用了mod_rewrite并删除了 .php 扩展名是可行的,所以我知道这不是问题.

I've looked all over and have yet to figure out how to make this work, I'm sure it's very simple for someone who knows what they're doing. For starters, I have made sure that mod_rewrite is enabled and removing .php extensions is working so I know that isn't the issue.

目前,我正在一个论坛上工作,而我想做的只是删除URL的?id = 1 方面,因此基本上使URL看起来像这样:

Currently I'm working on a forum, and what I'm trying to do is simply remove the ?id=1 aspect of the URL, so basically making the URL look like such:

http://url.com/Forum/Topic/1

代替

http://url.com/Forum/Topic?id=1

/Forum 是一个目录,其中包含名为 Topic.php

/Forum is a directory with a document named Topic.php

我当前的 .htaccess 如下:

Options -MultiViews
RewriteEngine on

RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]

任何帮助将不胜感激.

推荐答案

假定您已将应用程序中的URL更改为使用 http://example.com/Forum/Topic/1 ,然后尝试以下操作:

Assuming you've changed the URLs in your application to use http://example.com/Forum/Topic/1 then try the following:

# Remove .php file extension on requests
RewriteRule ^(.+)\.php$ /$1 [R,L]

# Specific rewrite for /Forum/Topic/N
RewriteRule ^(Forum/Topic)/(\d+)$ $1.php?id=$2 [END]

# Append .php extension for other requests
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ /$1.php [END]

您检查过%{REQUEST_FILENAME} .php 的原始条件不一定要检查您要重写的"URL",具体取决于请求的URL.

Your original condition that checked %{REQUEST_FILENAME}.php isn't necessarily checking the same "URL" that you are rewriting to - depending on the requested URL.

更新:但是,我将如何添加另一个ID变量,例如制作 http://example.com/Forum/Topic/1?page=1 看起来像 http://example.com/Forum/Topic/1/1

UPDATE: however how would I go about adding another ID variable, as in making http://example.com/Forum/Topic/1?page=1 look like http://example.com/Forum/Topic/1/1

因此,换句话说,/Forum/Topic/1/2 转到/Forum/Topic.php?id=1&page=2 .您可以添加另一个规则.例如:

So, in other words /Forum/Topic/1/2 goes to /Forum/Topic.php?id=1&page=2. You could just add another rule. For example:

# Specific rewrite for /Forum/Topic/N
RewriteRule ^(Forum/Topic)/(\d+)$ $1.php?id=$2 [END]

# Specific rewrite for /Forum/Topic/N/N
RewriteRule ^(Forum/Topic)/(\d+)/(\d+)$ $1.php?id=$2&page=$3 [END]

或者,您可以将它们组合为一条规则.但是,这意味着当省略第二个参数时,您将获得一个空的 page = URL参数(尽管不会成为问题).

Alternatively, you can combine them into one rule. However, this will mean you'll get an empty page= URL parameter when the 2nd paramter is omitted (although that shouldn't be a problem).

# Specific rewrite for both "/Forum/Topic/N" and "/Forum/Topic/N"
RewriteRule ^(Forum/Topic)/(\d+)(?:/(\d+))?$ $1.php?id=$2&page=$3 [END]

(?:/(\ d +))?部分与可选的第二个参数匹配.括号内的?:使其成为非捕获组(否则,我们最终得到一个与/2 匹配的捕获子模式)和尾随的?将整个组设为可选.

The (?:/(\d+))? part matches the optional 2nd parameter. The ?: inside the parenthesis makes it a non-capturing group (otherwise we end up with an additional capturing subpattern that matches /2) and the trailing ? makes the whole group optional.

这篇关于如何使用.htaccess重写URL(删除ID?= 1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆