[英]How to rewrite URLs with .htaccess (removing id?=1)
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.对于初学者,我已经确保启用了 mod_rewrite 并且删除了
.php
扩展名是有效的,所以我知道这不是问题。
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:目前我正在一个论坛上工作,我想要做的只是删除 URL 的
?id=1
方面,因此基本上使 URL 看起来像这样:
http://url.com/Forum/Topic/1
Instead of代替
http://url.com/Forum/Topic?id=1
/Forum
is a directory with a document named Topic.php
/Forum
是一个目录,里面有一个名为Topic.php
的文档
My current .htaccess
looks like this:我当前的
.htaccess
看起来像这样:
Options -MultiViews
RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
Any help would be appreciated.任何帮助,将不胜感激。
Assuming you've changed the URLs in your application to use http://example.com/Forum/Topic/1
then try the following:假设您已将应用程序中的 URL 更改为使用
http://example.com/Forum/Topic/1
然后尝试以下操作:
# 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]
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.您检查
%{REQUEST_FILENAME}.php
原始条件不一定检查您要重写的相同“URL” - 取决于请求的 URL。
UPDATE: however how would I go about adding another ID variable, as in making
http://example.com/Forum/Topic/1?page=1
look likehttp://example.com/Forum/Topic/1/1
更新:但是我将如何添加另一个 ID 变量,例如使
http://example.com/Forum/Topic/1?page=1
看起来像http://example.com/Forum/Topic/1/1
So, in other words /Forum/Topic/1/2
goes to /Forum/Topic.php?id=1&page=2
.因此,换句话说
/Forum/Topic/1/2
转到/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]
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).但是,这意味着当省略第二个参数时,您将获得一个空的
page=
URL 参数(尽管这应该不是问题)。
# Specific rewrite for both "/Forum/Topic/N" and "/Forum/Topic/N"
RewriteRule ^(Forum/Topic)/(\d+)(?:/(\d+))?$ $1.php?id=$2&page=$3 [END]
The (?:/(\\d+))?
(?:/(\\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 ?
括号内的
?:
使它成为一个非捕获组(否则我们最终会得到一个匹配/2
的附加捕获子模式)和尾随的?
makes the whole group optional.使整个组可选。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.