将.php重定向到无扩展名,并在不使用.php的情况下创建网址

时间:2012-12-25 12:23:42

标签: apache .htaccess mod-rewrite apache2

我真的不太了解apache中的Rewrite,但我的.htaccess文件存在问题。

原始.htaccess是

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L]


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


# Unless directory, remove trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.domain.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.domain.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]


ErrorDocument 404 /404.html

第一次重写是针对虚荣网址,另一种是将非www重定向到www,然后将.php重定向到扩展名为less并将.php文件重定向到扩展名为

我面临的问题是,如果我要求www.domain.com/ttt.php并且如果ttt不存在那么它会将我重定向到http://www.domain.com/profile?id=ttt.php 此外,我希望重写是为其他类型的PHP文件,如果我有ttt.png文件,我打开www.domain.com/ttt它打开图像,而不是显示404错误。

与其他类型的文件类似。如果名称很常见,它首先打开.txt文件。

感谢您对此进行任何更改,严重的是我不想弄乱我的网站。

1 个答案:

答案 0 :(得分:2)

这条规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L]

需要处于最后阶段。

这条规则:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

需要[L]标记,需要替换RewriteRule ^([^/.]+)$ $1.php [L]。然后,您可以按照优先顺序将.php替换为.png.txt以及您要处理的所有其他扩展程序:

Options +FollowSymLinks -Multiviews

RewriteEngine on

# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


# Unless directory, remove trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.domain.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.domain.com/$1 [R=301,L]

# add extension if the php file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

# add extension if the png file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png [L]

# add extension if the txt file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.txt -f
RewriteRule ^(.*)$ $1.txt [L]

# add extension if the html file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]

# etc.

# finally, route to profile.php if all else fails
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L]

ErrorDocument 404 /404.html

由于您要转到profile.php,在profile.php脚本中,如果/404.html参数不存在,则需要重定向到id(意思是获得路由的URI需要是404)。

相关问题