开发者

PHP: URL format www.mysite.com/users/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?


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜