[英]Mod_rewrite - Create Clean URLs
我们想为我们的网站创建干净的URL。 我已经搜索过如何做到这一点,并且找到了一个代码段,但是它不能满足我的要求。 在这里,我将解释我们的基本需求。
这是基本网站的URL: http : //www.test.com
我们有一个“页面”菜单,其中列出了用户可以看到的特定页面。 看起来如下: http : //www.test.com/pages.php
我们基本上希望当有人访问此/pages.php时 ,应将其重定向到/ pages (或使用完整URL: test.com/pages )或不重定向,仅显示未找到的页面。
它还应使用以下任何可能的配置:
我看过Stackowerflow的系统,它正在按照我们实际想要的方式工作。 所以每次我看其中一个:
这基本上是我所需要的。 我已经尝试过此代码,但是不能完全满足我的需要:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
如果有人可以写一个清晰的解释如何实现这一目标,我将感到高兴。 另外,如果您发现该帖子重复,请把我的链接链接为EXACT,因为我一直在搜索这个确切的问题,但是找不到任何答案。
谢谢
在下面,第一个规则将301将* .php重定向到不带.php的URL,第二个规则将通过向其添加“ .php”来处理所有其他URL:
RewriteCond %{REQUEST_URI} ^/(.*?)\.php$
RewriteRule ^/.*$ /%1 [R=301,QSA,L]
RewriteRule ^/(.*)$ /$1.php
例如,
/page.php
将重定向到/page
并处理page.php
。
好吧,这样写。
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName deli.localhost
DocumentRoot /var/www/deli/
<Directory /var/www/deli/>
Options -Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
RewriteEngine on
## Externally redirect clients directly requesting .(php|aspx|html|htm|pl|cgi|asp)+ page
## URIs to extensionless URIs
## If client request header contains php,aspx,html or htm file extension, redirect
## It avoids an infinite loop since "THE_REQUEST" is what the UA asks for, not what the
## server translates it to on the second pass.
## THE_REQUEST syntax is something like: GET /page.html HTTP/1.0
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.(php|aspx|html|htm|pl|cgi|asp)+(/.*)?\ HTTP [NC]
RewriteRule ^([^.]+)\.(php|aspx|html|htm|pl|cgi|asp)+(/.*)?$ http://xyz.com/$1$3 [NC,R=301,L]
FileETag none
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
ErrorDocument 503 /503.php
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
在另一个帖子上找到了这个。 显然,它去除了扩展名.php,.html,.cgi。
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*)/$ $1.html [L]
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.cgi -f
RewriteRule (.*)/$ $1.cgi [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f [OR]
RewriteCond %{REQUEST_FILENAME}\.php -f [OR]
RewriteCond %{REQUEST_FILENAME}\.cgi -f
RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.