How to use RewriteRule like WordPress and Drupal

牧云@^-^@ 提交于 2019-12-24 10:33:28

问题


I have a website with an include script that will have a various number of variables.

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

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)

How can I do this in the .htaccess-file? The site is not the root on the domain, but a couple of folders down.


回答1:


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

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>

Read more:

  • 5 Useful URL Rewriting Examples Using .htaccess
  • URL Rewriting Via .htaccess
  • mod_rewrite Cheat Sheet
  • and Google


来源:https://stackoverflow.com/questions/718695/how-to-use-rewriterule-like-wordpress-and-drupal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!