简体   繁体   English

使用 .htaccess 重写 URL

[英]Rewriting URLs with .htaccess

I am trying to convert urls like this http://example.com/page.php?user=x&post=x into http://example.com/blogs/user/post我正在尝试将这样的网址http://example.com/page.php?user=x&post=x转换为http://example.com/blogs/user/post

This is the code I have so far, not sure if I'm missing something or have done something wrong as it haven't been successful so far.这是我到目前为止的代码,不确定我是否遗漏了什么或做错了什么,因为它到目前为止还没有成功。

RewriteEngine On
RewriteRule ^blogs/([a-z0-9\-]+)/([0-9]{1,11})/$ page.php?user=$1&post=$2 [L]

EDIT: After making some amendments, it returns now an error 404 error saying page.php is not found.编辑:进行一些修改后,它现在返回一个错误 404 错误,表示page.php I know for certain the file is there.我肯定知道文件在那里。

2ND EDIT: Resolved 404 issue.第二次编辑:解决了 404 问题。

The post parameter is optional as well. post参数也是可选的。
user can will have a mix of A-z0-9 (no character limit) user可以混合使用 A-z0-9(无字符限制)
post can only be 0-9 with and upto 11 characters in length post只能是 0-9 和最多 11 个字符的长度

According to http://htaccess.madewithlove.be/ , this should work:根据http://htaccess.madewithlove.be/ ,这应该有效:

RewriteEngine On
RewriteRule ^blogs/([a-z0-9-]+)/([0-9]+)$ /page.php?user=$1&post=$2 [L]

This transforms http://example.com/blogs/abc_123/67890 to http://example.com/page.php?user=abc_123&post=67890 .http://example.com/blogs/abc_123/67890 http://example.com/page.php?user=abc_123&post=67890转换为http://example.com/page.php?user=abc_123&post=67890

The most notable change is that you apparently can't have a leading / in the first component of the rule, but I also removed the {11} constraint on the post ID since it's unlikely that would have the intended effect.最显着的变化是您显然不能在规则的第一个组件中使用前导/ ,但我还删除了帖子 ID 上的{11}约束,因为这不太可能产生预期的效果。

Try尝试

RewriteRule ^blogs/?([a-z0-9]*)?/?$ page.php?user=$1&post=$2 [NC,L]

The ? ? indicates that the matching is optional.表示匹配是可选的。 Therefore for /?因此对于/? the slash is optional.斜线是可选的。

And for the second section after the ^blogs/?对于^blogs/?之后的第二部分^blogs/?

 ([a-z0-9]*)?

If you remove the ?如果删除? and * from the above to become like this :*从上面变成这样:

RewriteRule ^blogs/?([a-z0-9])/?$ page.php?user=$1&post=$2 [NC,L]

The user will no longer become optional and must be fully matched. user将不再是可选的,必须完全匹配。 Example http://blogs/John ;示例http://blogs/John ; If you put + in place of * like below:如果你把+代替 * 如下:

RewriteRule ^blogs/?([a-z0-9]+)/?$ page.php?user=$1&post=$2 [NC,L]

The user need to be matched with at least 1 character. user需要匹配至少 1 个字符。 For example : http://blogs/J例如: http://blogs/J

And if you put * asterisk like below:如果你把*星号如下:

RewriteRule ^blogs/?([a-z0-9]*)/?$ page.php?user=$1&post=$2 [NC,L]

It can match to zero length.它可以匹配到零长度。 For example : http://blogs/ .例如: http://blogs/ Almost like optional.几乎就像可选的。

If the match failed you will get the 404 Not found error如果匹配失败,您将收到404 Not found error

This code provides the desired results...此代码提供了所需的结果...

RewriteRule ^blogs/([a-z0-9]+)?/?$ page.php?user=$1 [NC,L]
RewriteRule ^blogs/([a-z0-9]+)?/?([0-9]+)?/?$ page.php?user=$1&post=$2 [NC,L]

I also amended the post ID constraint as Fraxtil pointed out.正如 Fraxtil 指出的那样,我还修改了帖子 ID 约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM