【问题标题】:PHP: URL format www.mysite.com/users/33PHP:网址格式 www.mysite.com/users/33
【发布时间】:2011-03-01 13:39:26
【问题描述】:

我见过这样的 URL,我只是想知道它们是如何使用的。

到目前为止,我一直在使用 www.mysite.com/users/?id=33

如何使用其他格式?

【问题讨论】:

    标签: php friendly-url


    【解决方案1】:

    我知道您已经为这个问题选择了一个答案,但知道这一点非常有用。 PHP 框架 Codeigniter 默认允许您使用这样的 URL。我发现以这种方式进行 URL 重写要容易得多。有关这方面的更多信息,包括代码示例,请访问http://codeigniter.com/user_guide/general/urls.html

    【讨论】:

      【解决方案2】:

      如果有点冗长,Paul Peelan 的回答是正确的 :-) 将其放入您网站根目录的 .htaccess 文件中:

      RewriteEngine On
      RewriteRule ^users/(\d+)$ /users/?id=$1
      

      这将匹配 /users/33、/users/1、/users/12345 等并重定向到 /users/?id=12345。

      这要求您的 Apache 配置启用 mod_rewrite 引擎。请参阅the mod_rewrite docs 了解更多信息。

      【讨论】:

      • 感谢richsage,正是我所需要的!
      • 我想我需要在 httpd.conf 中启用 RewriteEngine 但我不确定在哪里可以找到它。我使用Mac顺便说一句。谢谢
      • 不确定它在 Mac 上的位置,但需要 LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so 之类的东西。您可能需要专门启用 Rewrite 模块 - 如果您遇到问题,serverfault.com 可能是询问服务器配置问题的更好地方:-)
      • 干杯!这适用于数字,但如果我想用文本替换它就不行。这是因为正则表达式中的 \d+ 吗?
      • ^Profile/([a-zA-Z0-9]+)$ Profile/?id=$1
      【解决方案3】:

      您将不得不使用“mod-rewrite”,例如.htaccess 文件。 然后 Apache 将根据您在 .htaccess 文件中的设置发送 url。

      例如:

      各种重写规则。

      <IfModule mod_rewrite.c>
        RewriteEngine on
      
        # If your site can be accessed both with and without the 'www.' prefix, you
        # can use one of the following settings to redirect users to your preferred
        # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
        #
        # To redirect all users to access the site WITH the 'www.' prefix,
        # (http://example.com/... will be redirected to http://www.example.com/...)
        # adapt and uncomment the following:
        # RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
        # RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
        #
        # To redirect all users to access the site WITHOUT the 'www.' prefix,
        # (http://www.example.com/... will be redirected to http://example.com/...)
        # uncomment and adapt the following:
        # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
        # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
      
        # Modify the RewriteBase if you are using Drupal in a subdirectory or in a
        # VirtualDocumentRoot and the rewrite rules are not working properly.
        # For example if your site is at http://example.com/drupal uncomment and
        # modify the following line:
        # RewriteBase /drupal
        #
        # If your site is running in a VirtualDocumentRoot at http://example.com/,
        # uncomment the following line:
        # RewriteBase /
      
        # Rewrite URLs of the form 'index.php?q=x'.
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
      </IfModule>
      

      问候, 保罗

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 2011-06-03
      相关资源
      最近更新 更多