繁体   English   中英

带有 .htaccess 的 SEO 友好 URL

[英]SEO Friendly URL's with .htaccess

在发布此内容之前环顾四周,找不到与我的特定问题相匹配的任何内容。

在我的网站上,我更喜欢我的 URL 中没有 .php,所以我在我的 .htaccess 文件中设置了它并且它完美地工作。 目前,我的网站上有一个名为“专辑”的页面,这是目前年度最佳专辑。 但是,我也想在明年做,等等。 现在,我可以将其作为专辑?date=2017,但这看起来很难看。 我想将其作为 example.com/albums/2017 进行,因为它看起来更好,而且对 SEO 更友好。

我的 HTACCESS 中的内容如下:

RewriteEngine On

# Remove PHP from file names
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

# Redirect albums.php?date=2017 to albums/2017
RewriteCond %{THE_REQUEST} \s/albums\?date=([0-9]+)\s [NC]
RewriteRule ^ /albums/%1? [R=301,L]
RewriteRule ^albums/([0-9]+)$ /albums?date=$1 [L]


Options -Indexes

# Error Document Redirects
ErrorDocument 403 http://example.com/errors/403
ErrorDocument 404 http://example.com/errors/404

albums.php?date=2017 在没有重定向的情况下工作,但是当我放入重定向时,它会将您发送到 /albums/2017,但在页面上没有显示任何内容。 如果我删除“从文件名中删除 PHP”重定向,它会在页面上显示信息。 很明显,我已经确定该错误是由于文件的“从文件名中删除 PHP”部分引起的,我只是不确定如何修复它。

我对 .htaccess 和 RewriteEngine 不是很了解,只知道足够或非常基本的东西,如果这看起来很糟糕,请道歉。 我已经尝试确保我没有提出重复问题,但如果我有,抱歉,

像这样试试,

RewriteEngine On
# removed .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)$ $1.php

# you can directly use album/(date) in your url using this rule
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteRule ^([\w-]+)/([\d]+)$ $1.php?date=$2

弄清楚了。

# Redirect albums.php?date=2017 to albums/2017
RewriteRule ^albums/([^/]*)$ /albums?date=$1 [L]

# Remove PHP from file names
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM