关于.htaccess的3个问题

时间:2013-12-11 17:26:09

标签: php apache .htaccess mod-rewrite redirect

我目前有以下.htaccess片段,我根本不理解它,我试图解决一些问题:

1 - 如何制作www.domain.co.uk和domain.co.uk以及最后用/index.php重新指向主页?因为我听说它对SEO不利。

2 - 我想删除任何URL之后的.php(这已经完成了,但我不知道上面的代码片段中是哪一行)。

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]

新 - 我们正在通过以下评论和答案进行研究:

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]

3 个答案:

答案 0 :(得分:2)

  1. 这对SEO来说并不坏。如果您不希望/index.php到您的网址,请不要以这种方式链接。从链接中删除/index.php。

  2. 这些行将“.php”添加到文件请求中。因此,如果您链​​接http://www.loaidesign.co.uk/somefile,服务器将请求 http://www.loaidesign.co.uk/somefile.php。它也可能将somefile.css重定向到somefile.css.php,但仅当somefile.css.php存在时才会重定向。

    # add .php to access file
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.+)$ $1.php [L]
    
  3. 重写您的网址生成代码,以您想要的格式输出链接(www.loaidesign.co.uk/projects/Wix_Websites)并将其添加到您的.htaccess

    RewriteRule ^projects/(.*)$ projects.php?project=$1 [L]
    

    然后你可以照常访问你的_GET($ _GET ['project']将等于obove示例中的Wix_Websites)

答案 1 :(得分:1)

您的代码肯定很有趣。您拥有的规则可以自行运行,但在一起使用时会造成混乱。问题是您使用的R标志没有L标志。您正在使用完整网址和普通网址的混合。

example.com/otherpage.php的请求最终会被重写为:

1. 301: http://www.example.com/http://example.com/home/yourhome/public_html/otherpage
2. 302: http://www.example.com/404.php
3. 301: http://www.example.com/404
4. 200: (and finally this page gives a 200 - OK status code)

example.com/index.php的请求遵循更奇怪的模式:

1. 301: http://www.example.com/http://example.com/home/yourhome/public_html/
2. 301: http://www.example.com/http:/example.com/home/yourhome/public_html
3. 302: http://www.example.com/404.php
4. 301: http://www.example.com/404
5. 200: (and finally this page gives a 200 - OK status code)

那么如何解决这个问题呢?请参阅以下.htaccess(将评论放在里面以便于理解)

DirectoryIndex index.html index.php
#/404.php redirects to /404, so why not do this immediately?
ErrorDocument 404 /404
ErrorDocument 504 /504

#These do nothing useful, so remove them
#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^loaidesign\.co\.uk$ [OR]
#RewriteCond %{HTTP_HOST} ^www\.loaidesign\.co\.uk$

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
# There is no point in escaping a dot in a string
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !(/|\.php)$
RewriteRule (.*) $1.php [L]

#The regex part of the rewritecondition must escape the dots.
RewriteCond %{HTTP_HOST} ^loaidesign\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.loaidesign.co.uk/$1 [R=301,L]

确保您的/404.php发送实际的404 Not Found标头。 200 OK标头向页面存在的状态(例如搜索引擎)说明。您的/504.php页面也是如此。除此之外,你的504.php会发出几个警告。

答案 2 :(得分:0)

更改(新代码)

RewriteCond %{HTTP_HOST} ^loaidesign\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.loaidesign\.co\.uk$

RewriteBase /

(即,部分恢复原件)。那两个RewriteConds什么也没做。

对于(1),你的旧的最后两行是正确的添加www。任何缺少它的域名:

RewriteCond %{HTTP_HOST} ^loaidesign\.co\.uk  [NC]
RewriteRule ^(.*)$  http://www.loaidesign.co.uk/$1 [R=301,L]

对于(2),你想要一个更多的SEO / SEF网址,没有.php?您删除.php 其他地方,例如您的链接或用户键入的内容,然后将添加 .php重新添加到.htaccess中,以便服务器随后使用filename.php。删除

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

并将其替换为

# add .php to any suspected file lacking one
RewriteCond  %{REQUEST_FILENAME}  !-f
RewriteCond  %{REQUEST_FILENAME}  !-d
RewriteCond  %{REQUEST_FILENAME}.php  -f
RewriteRule  ^(.*)$  $1.php  [L]

请注意,此 重写 URI ...它不会显示在地址栏中,因为没有30x的返回代码。

Number(3)是标准的SEO编码。首先,您的链接必须以/projects/Wix_Websites格式生成 - .htaccess不会这样做。在.htaccess中,您将其转换回服务器理解的表单:/projects.php?project=Wix_Websites。问题是:你想要做到这一点有多普遍?您希望任何 /XX/AA成为XX.php?YY=AA,还是只需要像项目这样的特定内容?

“删除斜杠,如果不是目录”我认为将会起作用,因为如果你想把它放回去。