htaccess 在 php 中重写 URL

发布于 2024-12-12 05:53:01 字数 455 浏览 0 评论 0原文

请帮忙在php中通过htaccess将动态url转换为静态url。

示例 1:

www.example.com/say.php?name=ksrtc-palakkad-kerala&id=c669239(动态 URL)

www.example.com/say/ksrtc-palakkad- kerala-c669239.html(静态 URL)


示例 2:

www.example.com/index.php?name=ksrtc-palakkad-kerala&id=c669239(动态网址)

www.example.com/say/ksrtc-palakkad-kerala-c669239 .html(静态 URL)

问候, 普拉巴特

Please help to convert dynamic url to static url by htaccess in php.

Example 1:

www.example.com/say.php?name=ksrtc-palakkad-kerala&id=c669239 (Dynamic URL)

www.example.com/say/ksrtc-palakkad-kerala-c669239.html (Static URL)


Example 2:

www.example.com/index.php?name=ksrtc-palakkad-kerala&id=c669239 (Dynamic URL)

www.example.com/say/ksrtc-palakkad-kerala-c669239.html (Static URL)

Regards,
Prabhat

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

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

发布评论

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

评论(1

中二柚 2024-12-19 05:53:01

您应该创建一个如下所示的 .htaccess:

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php [L]

现在完整路径将被重定向到 index.php - 这将是您的单个入口点,您可以通过 $_SERVER['REQUEST_URI'] 如果我没记错的话。

现在,您的工作是编写一组规则来了解此 url 匹配的所有可能的路由(正则表达式),然后解析 url 并相应地设置 GET 变量(来自第二个示例 - name、id)。如果该 url 与任何已知 url 都不匹配 - 将其转发到您的错误页面。

而且你还应该进行反向路由,如果有人打开一个带有简单获取变量的网址,例如“index.php?name=ksrtc-palakkad-kerala&id=c669239”,你应该使用 301 永久重定向重定向到“SEO”网址“ /say/ksrtc-palakkad-kerala-c669239.html"

如果这不能回答您的问题,请详细说明。

You should create an .htaccess that looks something like this:

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php [L]

Now the full path would be redirected to index.php - this will be your single entry point, you could access it through $_SERVER['REQUEST_URI'] if I recall well.

Now it will be your job to write set of rules to know all the possible routes that this url matches (regular expressions), then parse the url and set the GET variables accordingly (from your second example - name, id). If the url doesn't match any of the known urls - forward it to your error page.

And also you should do reverse-routing, if someone opens an url with plain get variables like "index.php?name=ksrtc-palakkad-kerala&id=c669239" you should redirect with 301 PERMANENT REDIRECT to the "SEO" url "/say/ksrtc-palakkad-kerala-c669239.html"

If this doesn't answer your question, please elaborate.

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