重写为“漂亮的 URL”?

发布于 2024-08-21 23:11:13 字数 1459 浏览 4 评论 0原文

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

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

发布评论

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

评论(4

删除会话 2024-08-28 23:11:13

如果您希望 /company.php?test=AAA001 重定向到 /company/AAA001,请执行以下操作:

RewriteCond %{QUERY_STRING} test=([A-Z]+[0-9]+)  
RewriteRule ^company.php /company/%1? [R]    

如果您希望 /company/AAA001要重写为 /company.php?test=AAA001,请执行以下操作:

RewriteRule company/([A-Z]+[0-9]+)$ /company.php?test=$1

If you want /company.php?test=AAA001 to redirect to /company/AAA001, do this:

RewriteCond %{QUERY_STRING} test=([A-Z]+[0-9]+)  
RewriteRule ^company.php /company/%1? [R]    

If you want /company/AAA001 to be rewritten as /company.php?test=AAA001, do this:

RewriteRule company/([A-Z]+[0-9]+)$ /company.php?test=$1
倚栏听风 2024-08-28 23:11:13

我认为你的 RewriteRule 是以错误的方式编写的,有点倒退,它应该是

RewriteRule company.php?test=([A-Z]+)([0-9]+) /company/AAA$2

I.e.你首先给出要匹配的模式,然后将其重写为......但是我不确定你是否真的可以像这样匹配 GET 参数......

I think your RewriteRule is written in the wrong way, kind of backwards, it should be

RewriteRule company.php?test=([A-Z]+)([0-9]+) /company/AAA$2

I.e. you first give the pattern to match and then what it is rewritten to... however I'm not sure if you can actually match the GET parameters like this...

水染的天色ゝ 2024-08-28 23:11:13

我不是 mod_rewrite 专家,但是:

RewriteRule company/(.*)/(.*)/$ /company.php?$1=$2

不应该匹配这样的表达式:

/company/foo/bar/

并将它们映射到:

/company.php?foo=bar

你的 URL 中只是:

/company/foo

Apache 日志说了什么?
你的 .htaccess 真的被读取了吗?
您重新加载 Apache 配置了吗? (不记得是否需要)

I'm not a mod_rewrite expert, but:

RewriteRule company/(.*)/(.*)/$ /company.php?$1=$2

shouldn't match expressions like:

/company/foo/bar/

and map them into:

/company.php?foo=bar

You have, in your URL, just:

/company/foo

What are the Apache logs saying?
Is your .htaccess actually read?
Did you reload the Apache configuration? (can't remember if it is needed)

疯了 2024-08-28 23:11:13

您的顺序错误...紧接在“RewriteRule”之后的是您想要的 URL 形式(即您的干净 url),以及该 url 中可能发生更改的正则表达式。之后,您将获得 URL 的路径以及找到的值的正则表达式标记(即 $1、$2 等)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

ErrorDocument 400 http://www.domain.com/400
ErrorDocument 403 http://www.domain.com/403
ErrorDocument 404 http://www.domain.com/404
ErrorDocument 500 http://www.domain.com/500

RewriteEngine on
RewriteBase /
#PAGES

RewriteRule ^/company/([A-Z]+)([0-9]+)/$ company.php?test=$1&%{QUERY_STRING} [NC,L]

You have the wrong order... immediately after "RewriteRule" is the form of the URL that you want (ie. your clean url), with the Regex for whatever may change in that url. After that, you have the path of the URL, plus the Regex tokens for the found values (ie. $1, $2, etc.)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

ErrorDocument 400 http://www.domain.com/400
ErrorDocument 403 http://www.domain.com/403
ErrorDocument 404 http://www.domain.com/404
ErrorDocument 500 http://www.domain.com/500

RewriteEngine on
RewriteBase /
#PAGES

RewriteRule ^/company/([A-Z]+)([0-9]+)/$ company.php?test=$1&%{QUERY_STRING} [NC,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文