【发布时间】:2013-12-11 17:26:09
【问题描述】:
我目前有以下.htaccess sn-p,完全看不懂,尝试简单解决几个问题:
1 - 如何使 www.domain.co.uk 和 domain.co.uk 以及最后 /index.php 都重新定向到主页?据我所知,这对 SEO 不利。
2 - 我想删除任何 URL 之后的 .php(这已经完成,但我不知道上面 sn-p 中的哪一行)。
3 - 我有一个要添加项目的数据库,它会为我添加的项目生成 URL,其中一个 URL 是“http://www.domain.co.uk/projects.php?project=Websites”如何摆脱“.php?project=”并制作它看起来像 www.domain.co.uk/projects/Websites
DirectoryIndex index.html index.php
ErrorDocument 404 http://www.domain.co.uk/404.php
ErrorDocument 504 /504.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.co\.uk$
RewriteEngine On
RewriteBase /
# remove enter code here.php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{HTTP_HOST} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,L]
新 - 我们正在根据以下 cmets 和答案进行处理:
DirectoryIndex index.html index.php
ErrorDocument 404 /404
ErrorDocument 504 /504
RewriteEngine On
RewriteBase /
# remove enter code here.php; use THE_REQUEST to prevent infinite loops
# By puting the L-flag here, the request gets redirected immediately
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301,L]
# remove index
# By puting the L-flag here, the request gets redirected immediately
# The trailing slash is removed in a next request, so be efficient and
# dont put it on there at all
RewriteRule (.*)/index$ $1 [R=301,L]
# remove slash if not directory
# By puting the L-flag here, the request gets redirected immediately
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301,L]
# add .php to access file, but don't redirect
# On some hosts RewriteCond %{REQUEST_FILENAME}.php -f will be true, even if
# no such file exists. Be safe and add an extra condition
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !(/|\.php)$
RewriteRule (.*) $1\.php [L]
#Hey, this one is fine ;-)
RewriteCond %{HTTP_HOST} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,L]
【问题讨论】:
-
你还有什么规定吗?
-
没有伴侣,这就是全部:/所有这些行有点混乱,我不知道从哪里开始
-
第二个
RewriteEngine on上方的双RewriteEngine on和孤儿RewriteCond从何而来?从删除那些开始。 -
哦,刚刚做了,看看上面 - 有一个新的部分将用于新代码
-
我在实际站点上做了一些测试,它重定向我的 url 也很有趣。我会为你写一个答案:P
标签: php apache .htaccess mod-rewrite redirect