Apache重写规则(url重写)

发布于 2024-10-06 09:13:50 字数 189 浏览 0 评论 0原文

例如,如何进行网址重写,我希望我的看起来像

www.myurl.com/study-abroad/what/ever/

如果我输入

www.myurl.com/studyabroad/what/ever/

想法是有留学而不是留学

How can make a url rewriting for example, I want my to look like

www.myurl.com/study-abroad/what/ever/

If I type

www.myurl.com/studyabroad/what/ever/

The idea is to have study-abroad instead of studyabroad

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

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

发布评论

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

评论(2

裂开嘴轻声笑有多痛 2024-10-13 09:13:50

如果您提前知道每个 URL 将遇到多少个破折号,则可以完成此操作:

RewriteEngine On

# Replacing a single dash, e.g. example.com/study-abroad/what/ever
RewriteCond %{REQUEST_URI} ^([^-]*)-([^-]*)$
RewriteRule ^([^-]*)-([^-]*)$ $1$2 [L]

# Replacing two dashes, e.g. example.com/study-abroad/what-ever
RewriteCond %{REQUEST_URI} ^([^-]*)-([^-]*)-([^-]*)$
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ $1$2$3 [L]

...等等。不过,我可以补充一点,这是设计所破坏的,您应该重新考虑您的方法。

问候

It can be done if you know in advance with how many dashes per URL you'd be confronted:

RewriteEngine On

# Replacing a single dash, e.g. example.com/study-abroad/what/ever
RewriteCond %{REQUEST_URI} ^([^-]*)-([^-]*)$
RewriteRule ^([^-]*)-([^-]*)$ $1$2 [L]

# Replacing two dashes, e.g. example.com/study-abroad/what-ever
RewriteCond %{REQUEST_URI} ^([^-]*)-([^-]*)-([^-]*)$
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ $1$2$3 [L]

... and so forth. May I add, though, that this is broken by design and you should reconsider your approach.

Regards

鹤仙姿 2024-10-13 09:13:50

对于这样一个简单的示例,我将使用 mod_alias 重定向而不是 mod_rewrite。示例:

Redirect permanent /studyabroad http://www.myurl.com/study-abroad

请参阅 http://httpd.apache.org/docs/2.0/ mod/mod_alias.html#redirect 获取完整文档。

您需要将此(或类似的)代码片段放入 Apache 配置文件中:如果启用(并且您有适当的权限),这可能是您网站目录中的 .htaccess 。或者,您可以修改 Apache 主配置。请参阅http://httpd.apache.org/docs/current/configuring.html 有关 Apache 配置文件的信息。

(编辑以反转映射:添加破折号而不是删除它)

For such a simple example I would use mod_alias Redirects rather than mod_rewrite. Example:

Redirect permanent /studyabroad http://www.myurl.com/study-abroad

See http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirect for the full documentation.

You need to put this (or a similar) snippet into an Apache configuration file: if enabled (and if you have appropriate permissions), this might be .htaccess in your website's directory. Or, you could modify the master Apache config. See http://httpd.apache.org/docs/current/configuring.html for information about Apache configuration files.

(edited to invert the mapping: adding a dash instead of removing it)

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