重定向godaddy linux主机的.htaccess问题

时间:2013-12-05 16:34:50

标签: .htaccess

我的.htaccess遇到了一些问题,对此仍然是新手。具体来说,没有删除.php文件扩展名,并且写了blog / something / for article.php?title = something无法正常工作。如果您有任何其他提示或错误,请指出它们。

# generic rules
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# redirect to 404 page when requested file does not exist
ErrorDocument 404 /404.php

# ----------------------------------------------------- /
# URL rewrites
# ----------------------------------------------------- /

# http://www.example.com => http://example.com/ (remove the www)
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# http://example.com/index.php => http://example.com/ (remove index.php)
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ $1 [R=301,L]

# http://example.com/article.php?title=something => http://example.com/blog/something (user's link was for http://example.com/blog/something)
RewriteRule ^blog/([a-z0-9-]+)/$ /article.php?title=$1 [QSA]

# http://example.com/page.php => http://example.com/page/ (remove .php file extension)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

1 个答案:

答案 0 :(得分:0)

你的4个重写部分的评论充其量是误导性的,最坏的情况表明你不理解SEO URI的概念。

  1. 将www.domain.tld更改为domain.tld - 好的,标准做法

  2. 从INcoming URI中删除尾随index.php。我不确定这是什么意思,但你的代码应该有效。用户或搜索引擎应该看到没有index.php的地址。

  3. 您实际上正在将INCOMING /blog/..id ..更改为/article.php?title=..id ..一些注意事项:您的页面需要使用/ blog格式生成链接。 .htaccess只是将其更改为服务器可以消化的内容。其次,你需要一个尾随/在...之后...如果你想让它可选,使用/? 。第三,任何传入的查询字符串都将被丢弃,因为您指定了一个新的查询字符串(title =)并且没有[QSA]标志。第四,[L]是不必要的(但无害),因为RewriteCond没有生效。

  4. 再次
  5. ,可能会混淆INcoming URI和您正在将其更改为服务器可接受的形式。用户是否希望为您提供表单..page ../并且您将其转换为PHP文件..page.php?如果是这样,为什么尾随/?它告诉服务器这是一个目录,而不是一个文件。同样,在输入上,您需要尾随/(使用/?使其成为可选项)。

  6. 至少,您的评论需要澄清您期望从用户(或您网站上的链接)输入的内容,以及您将其转化为什么。