重写htaccess的清洁网址和自定义404页 [英] rewrite htaccess for clean url and custom 404 page
问题描述
我试图做四件事跟的.htaccess
I'm trying to do four things with .htaccess
第一件事情就是从所有文件删除PHP扩展
First thing is removing the php extension from all files
所以
www.mywebsite.com/home is written www.mywebsite.com/home.php
我想更改为错误页面,所以如果一个请求到www.mywebsite.com/adakadabra且有像adakabra.php自定义404.php这样的页面用于unstead。
I want to change to error page so if a request is made to www.mywebsite.com/adakadabra and there is such page like adakabra.php a custom 404.php is used unstead.
第三我想有虚荣的URL,这样用户就可以拥有像
Thirdly I would like to have vain url such that users can have profile links like
www.mywebsite.com/Joe
我有一个profile.php我用GET请求来获取用户名这样的重写是从 www.mywebsite.com/joe到www.mywebsite.com/profile.php?username=joe~~V
I have a profile.php and I use a get request to get the username so the rewrite would be from www.mywebsite.com/joe to www.mywebsite.com/profile.php?username=joe
最后但并非最不重要
我希望有一个网页一样www.mywebsite.com/p/123453
I would want to have a pages like www.mywebsite.com/p/123453
要重写 www.mywebsite.com/p.php?photo=123453
to rewrite to www.mywebsite.com/p.php?photo=123453
我有我的web服务器的页面p.php。
I have the page p.php on my webserver.
推荐答案
这可以给它,在/.htaccess 3套规则:
This can to it, with 3 sets of rules in /.htaccess:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([0-9]+)$ /p.php?photo=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Joe|Mao|Napoleon|Gandi)$ /profile.php?username=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ /$1.php [QSA,L]
这是如何工作的:
Here how it works:
- www.mywebsite.com/p/123453 => /p.php?photo=123453
- www.mywebsite.com/Joe => /profile.php?username=Joe
- www.mywebsite.com/home => /home.php, www.mywebsite.com/houra/foo/bar/home => /houra/foo/bar/home.php
- www.mywebsite.com/p/123453 => /p.php?photo=123453
- www.mywebsite.com/Joe => /profile.php?username=Joe
- www.mywebsite.com/home => /home.php, www.mywebsite.com/houra/foo/bar/home => /houra/foo/bar/home.php
这篇关于重写htaccess的清洁网址和自定义404页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!