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

发布于 2024-09-05 06:39:48 字数 99 浏览 6 评论 0原文

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

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

如何使用其他格式?

I've seen URL's like this around and I'm just wondering how it is they are used.

Until now I've been using www.mysite.com/users/?id=33

How can I use the other format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迟月 2024-09-12 06:39:48

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

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

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

这要求您的 Apache 配置启用了 mod_rewrite 引擎。有关详细信息,请参阅mod_rewrite 文档

Paul Peelan's answer is correct if a little verbose :-) Put this in your .htaccess file in the root of your site:

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

This will match /users/33, /users/1, /users/12345 etc and redirect to /users/?id=12345.

This requires that your Apache configuration has the mod_rewrite engine enabled. See the mod_rewrite docs for further information.

樱娆 2024-09-12 06:39:48

您将必须使用“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>

问候,
保罗

You will have to use the "mod-rewrite" and e.g. an .htaccess file.
Apache will then send the urls according to your settings in the .htaccess file.

E.g.:

Various rewrite rules.

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

Regards,
Paul

如若梦似彩虹 2024-09-12 06:39:48

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

I know you have already selected an answer for this question, but this is quite useful to know. The PHP framework Codeigniter allows you to use URLs like this by default. I found it a lot easier to do URL rewriting this way. Further information about this, including code samples, can be found at http://codeigniter.com/user_guide/general/urls.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文