【问题标题】:3 Issues Regarding .htaccess关于 .htaccess 的 3 个问题
【发布时间】: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


【解决方案1】:
  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. 重写您的 URL 生成代码以输出所需格式的链接 (www.loaidesign.co.uk/projects/Wix_Websites) 并将其添加到您的 .htaccess

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

    然后你可以像往常一样访问你的 _GET ($_GET['project'] 将等于上面示例中的 Wix_Websites)

【讨论】:

  • 请看一下更新后的版本,是这样的吗?
【解决方案2】:

您的代码当然很有趣。您拥有的规则可以单独使用,但一起使用时会造成混乱。问题是您使用的 R 标志没有 L 标志。您正在使用完整 url 和普通 url 的混合。

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(把cmets放在里面方便理解)

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 给出了几个警告。

【讨论】:

  • 请在我的问题中查看新部分的编辑,是这样吗?我现在明白你回答的大部分内容确实解释了很多 - 但是项目 URL 呢?
  • @Leo daker 对这部分问题给出了很好的答案,我觉得没有必要复制他。
  • 我理解队友但是我已经尝试过并在代码末尾添加 RewriteRule ^projects/(.*)$ projects.php?project=$1 [L] 但它什么也没做:/ live预览可以在这里找到:loaidesign.co.uk/projects.php?project=Wix_Websites
  • 自从你复制了我的答案后,我又对它进行了一次编辑。忘记了逃跑的问题。那和状态码。
  • 改变了,从项目页面看一切似乎都很好:/请帮助
【解决方案3】:

更改(新代码)

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 URL,而不需要 .php?您删除 .php elsewhere,例如您的链接或用户输入的内容,然后在 .htaccess 中将 add.php 放回此处,以便服务器可以使用文件名.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 返回码。

数字 (3) 是标准的 SEO 编码。首先,您的 链接必须以/projects/Wix_Websites 的格式生成它——.htaccess 不会这样做。在 .htaccess 中,您将其转换回服务器可以理解的形式:/projects.php?project=Wix_Websites。问题是:你想让这个通用到什么程度?您希望任何 /XX/AA 成为XX.php?YY=AA,还是只是特定的项目,如项目?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多