【问题标题】:using mod_rewrite to change url path使用 mod_rewrite 更改 url 路径
【发布时间】:2010-11-13 11:44:34
【问题描述】:

是否可以在 URL 中使文件夹透明,如下例所示: example.com/test/ 变成 example.com/

使用 mod_rewrite?

我喜欢有组织文件夹,但我想要一个漂亮干净的网址。

【问题讨论】:

  • 如何更改,使用重定向或内部重写?你能描述一下当用户输入一个 URL 和另一个 URL 时应该发生什么吗?
  • 您的意思是将root_folder/test/page.html 之类的文件作为example.com/page.html 提供吗?
  • @Pekka-internal rewrite 最好@Alberto-exactly

标签: apache .htaccess mod-rewrite


【解决方案1】:

您可以通过 mod_rewrite 将类似内容放入根文件夹的 .htaccess 文件中来实现:

RewriteEngine on

# the first condition is for avoiding an infinite loop
RewriteCond %{REQUEST_URI} !^/test/.*
RewriteRule ^(.*)$  /test/$1

这将重定向到/test// 请求的所有页面/文件。

如果您只想重定向某些文件,请使用更具体的规则:

RewriteEngine on

# the condition is for avoiding an infinite loop
RewriteCond %{REQUEST_URI} !^/test/.*
# redirecting only images files and files that start with "doc"
RewriteRule ^(.*\.(jpg|png|gif))$  test/$1 [L]
RewriteRule ^(doc.*)$  test/$1 [L]

或者如果你想使用不同的文件夹:

RewriteEngine on

# the condition is for avoiding an infinite loop
RewriteCond %{REQUEST_URI} !^/images/.*
RewriteCond %{REQUEST_URI} !^/docs/.*
# redirecting only images files and files that start with "doc"
RewriteRule ^(.*\.(jpg|png|gif))$  images/$1 [L]
RewriteRule ^(doc.*)$  docs/$1 [L]

【讨论】:

    猜你喜欢
    • 2012-09-25
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 2014-02-28
    • 2011-12-29
    • 2011-03-16
    • 2019-03-28
    • 2015-09-17
    相关资源
    最近更新 更多