Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

.htaccess - How to redirect all my urls with no extension to end with .php

I originally wanted to have all my urls end with no extension. Unfortunately, I've tried many htaccess codes and I've just about given up.

So now I want to make it so if a person wants to visit a page in my site, but forgets to enter .php, he/she will automatically be redirected to the same url but with the .php

How can this be done? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here are the rules:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
  1. It will check if requested resource is not a existing folder. For example: you requesting http://www.example.com/help. If there is such folder present (/help) the rule will do nothing (priority is given to a folder). If you do not want this behaviour then remove the first line.

  2. It will check if there is such .php file before rewriting. For example: you requesting http://www.example.com/aboutus but there is NO aboutus.php file there -- no rewrite will occur.

  3. All such requests should be without trailing slash: should be http://www.example.com/aboutus and NOT http://www.example.com/aboutus/

  4. The rule will work for URL in subfolders as well: e.g. http://www.example.com/pages/help/aboutus will be rewritten just fine.

  5. Because of the above checks the rule will not enter into a rewrite loop (no 500 error on this rule)

  6. Query string (page parameters) will be preserved


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...