PHP: формат URL www.mysite.com/users/33 - PullRequest
1 голос
/ 09 июня 2010

Я видел такие URL-адреса, и мне просто интересно, как они используются.

До сих пор я пользовался www.mysite.com/users/?id=33

.

Как я могу использовать другой формат?

Ответы [ 3 ]

3 голосов
/ 09 июня 2010

Ответ Пола Пилана правильный, если немного подробный :-) Поместите это в свой файл .htaccess в корне вашего сайта:

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

Это будет соответствовать / users / 33, / users / 1, / users / 12345 и т. д. и перенаправьте на /users/?id=12345.

Для этого необходимо, чтобы в конфигурации Apache был включен механизм mod_rewrite.Для получения дополнительной информации см. Документы mod_rewrite .

2 голосов
/ 09 июня 2010

Вы должны будете использовать «mod-rewrite» и, например, файл .htaccess.После этого Apache отправит URL-адреса в соответствии с вашими настройками в файле .htaccess.

Например:

Различные правила перезаписи.

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

С уважением, Пол

0 голосов
/ 09 июня 2010

Я знаю, что вы уже выбрали ответ на этот вопрос, но это очень полезно знать.PHP Framework Codeigniter позволяет вам использовать URL-адреса, подобные этому, по умолчанию.Мне было намного проще сделать переписывание URL таким способом.Дополнительную информацию об этом, включая примеры кода, можно найти по адресу http://codeigniter.com/user_guide/general/urls.html

...