PHP:URL格式www.mysite.com/users/33

时间:2010-06-09 09:03:34

标签: php friendly-url

我已经看过这样的网址了,我只是想知道它们是如何使用的。

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

如何使用其他格式?

3 个答案:

答案 0 :(得分:3)

保罗·皮兰的答案是正确的,如果有点冗长:-)把它放在你网站根目录的.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

答案 1 :(得分:2)

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

E.g:

各种重写规则。

<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>

此致 保罗

答案 2 :(得分:0)

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