【问题标题】:how to point url to a different directory如何将url指向不同的目录
【发布时间】:2015-04-16 04:11:13
【问题描述】:

请帮忙,我很困在这里。 我正在开发一个需要用户注册并在我的网站上拥有个人目录的网络应用程序。 我希望用户目录如下: http://example.com/theusername

我可以通过在我的站点根目录上为每个用户创建文件夹来完成工作。但这将使我的根目录保持集群状态。 我是否可以将用户目录设为:http://example.com/u/theusername,但只能通过http://example.com/theusername 对其进行评估??

谢谢。

【问题讨论】:

    标签: .htaccess url directory server


    【解决方案1】:

    是的,当然可以。您可以在 DOCUMENT_ROOT/.htaccess 文件中使用此代码:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_URI} !^/(index|excluded1|excluded2)\.php [NC]
    RewriteRule ^((?!u/).+)$ u/$1 [L,NC]
    

    您可以使用正则表达式替换在RewriteCond %{REQUEST_URI} 中包含您的排除项,或者使用更多RewriteCond %{REQUEST_URI} 行,如上所示。

    RewriteRule 在我的回答中使用了否定的前瞻(?!u/),这意味着只有在没有u/ 的情况下才匹配。操作是始终使用u/$1/username 路由到/u/username

    参考资料:

    【讨论】:

    • 感谢您的回答,但我对 .htaccess 语法并不了解。请详细说明。
    • 在我的回答中添加了解释和参考链接。让我知道这个答案是否适合你。
    猜你喜欢
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2019-04-29
    相关资源
    最近更新 更多