【问题标题】:How do I make a custom URL parser with Apache?如何使用 Apache 制作自定义 URL 解析器?
【发布时间】:2014-12-01 02:29:33
【问题描述】:

我听说这可以通过web.config 文件完成。我想这样做,例如,我的 URL http://help.BHStudios.org/site 可能会转到 http://BHStudios.org/help.php?section=site,或者 http://i.BHStudios.org/u3Hiu 可能会重定向到存储在数据库中的其他 URL,其中散列 u3Hiu 作为键,或者如果出现问题错误并且内部文件结构像 http://Kyli.BHStudios.org/http/bhstudios/v2/self/index.php 一样暴露出来(无论出于何种原因,GoDaddy 的服务器都会发生这种情况),它会在暴露给用户之前将其更改为预期的 URL http://Kyli.BHStudios.org

由于我以前从未这样做过,请你也解释一下你为什么给出你这样做的答案?

【问题讨论】:

    标签: apache url-rewriting web-config


    【解决方案1】:

    在您的服务器 httpd.conf 或 .htaccess 文件中的一些 Apache mod_rewrite rules,在您的 htdocs 目录中将完成您想要的大部分操作,例如

    RewriteEngine On
    RewriteBase  /
    
    # Default Rule - for non physical objects (not a file or directory): 
    # Internally rewrite (user won't see the URL) to /index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ /index.php [L]
    
    #If the Browser request contains a .php, instruct the browser to remove it.
    RewriteCond %{THE_REQUEST}      \.php      [NC]
    RewriteRule ^/?(.*)\.php$       http://%{HTTP_HOST}/$1         [R=301,NC,L]
    
    # Specific rule
    RewriteRule ^/?site   /help.php?section=site 
    

    真实文件系统对象的掩码并不完美,而且有点毫无意义,因为用户只需要在任何服务页面上右键单击并查看源代码,即可获取实际 URL。

    【讨论】:

    • 我可能无法通过 GoDaddy 访问它;我什至没有看到 htdocs 目录。如果我只是创建它,它会起作用吗?
    • 如果您有一个基于 Apache 的站点,那么可以,请参阅:support.godaddy.com/help/article/2504/what-is-htaccess
    • 是的,我有一个 .htaccess 文件,但没有 htdocs 目录。我现在能做什么?
    • 根据 GoDaddy 文本,将文件放在您的根目录中。
    • 有什么地方可以安全地测试它以确保它不会破坏任何东西吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2014-04-10
    • 2020-02-03
    • 2019-10-26
    • 1970-01-01
    • 2022-10-13
    相关资源
    最近更新 更多