如何使用WordPress和Drupal等RewriteRule [英] How to use RewriteRule like WordPress and Drupal
问题描述
我有一个带有包含脚本的网站,该脚本将包含各种变量.
I have a website with an include script that will have a various number of variables.
以下是一些类型的URL和变量:
Here are some of the types of URLs and variables:
Main page: index.php
Login page: login.php (redirects to index.php after login).
Logout page: logout.php (redirects to login after logout).
Profile page: index.php?id=profile
Users password page: index.php?id=profile&sub=password
Main page: index.php
Login page: login.php (redirects to index.php after login).
Logout page: logout.php (redirects to login after logout).
Profile page: index.php?id=profile
Users password page: index.php?id=profile&sub=password
有些页面甚至在URL中具有更多变量.我想使用RewriteRules来获得这样的漂亮URL:
And some pages even has more variables in the URL. I want to use RewriteRules to be able to get nice URLs like this:
url.com/my/site/ (base)
url.com/my/site/profile/ (profile page)
url.com/my/site/profile/password/ (password page)
url.com/my/site/profile/password/variable1/variable2/variable3/ (use variables in the URL)
url.com/my/site/ (base)
url.com/my/site/profile/ (profile page)
url.com/my/site/profile/password/ (password page)
url.com/my/site/profile/password/variable1/variable2/variable3/ (use variables in the URL)
如何在.htaccess文件中执行此操作?该站点不是域的根目录,而是几个文件夹.
How can I do this in the .htaccess-file? The site is not the root on the domain, but a couple of folders down.
推荐答案
.htaccess
规则适用于其容器目录及其所有子目录.这可能是一个很好的起点:
.htaccess
rules apply to its container directory and all of its sub directories. This might be a good starting point:
<IfModule mod_rewrite.c>
# Removing .php file extension.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
或者您可能要从网址中删除index.php的GET参数:
Or you might want to remove the index.php's GET parameters from within url:
<IfModule mod_rewrite.c>
# Removing GET parameters.
RewriteRule ^index/([^/\.]+)/?$ index.php?id=$1 [L]
</IfModule>