【问题标题】:seo friendly url for blogs博客的 seo 友好 URL
【发布时间】:2017-09-29 10:12:41
【问题描述】:

我正在构建一个博客系统,我在我的数据库中有一个名为 url 的表,它保存了带有日期的帖子标题我还有一个名为 index.php 的页面,其中显示了数据库中的所有数据,当点击获取时你到post.php。问题是该网址对 SEO 不友好site/post?url=seo-friendly-url.html/2017-09-29,我有一个运行良好的 .htaccess 文件

`Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
RewriteRule ^post/([a-zA-Z0-9-/]+)$ post.php?url=$1  
RewriteRule ^post/([a-zA-Z-0-9-]+)/ post.php?url=$1  
`

但拒绝重写我的博客文章,我的索引超链接看起来像这样<a href='post?url=".$row['url']."'>,已尝试将其重写为<a href='post/".$row['url']."'>但不工作,已经研究但仍然没有进展,请任何帮助会很大感谢,因为这几天一直在讨论这个问题。

【问题讨论】:

标签: php .htaccess mod-rewrite


【解决方案1】:

在您网站的 VirtualHost 文件中添加 AliasMatch(这将位于您的 apache 文件夹中)

这些是您需要在虚拟主机文件中添加的行

AliasMatch ^post/([a-zA-Z0-9-/]+)$ post.php?url=$1  
AliasMatch ^post/([a-zA-Z-0-9-]+)/ post.php?url=$1

在基于 linux 的服务器中。虚拟主机将位于文件夹中

/etc/apache2/sites-enabled/ 

在此文件夹中找到您的网站虚拟主机文件并添加这两行

虚拟主机会有类似这样的代码

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined


    #Add these two line only donot disturb any other lines 

    AliasMatch ^post/([a-zA-Z0-9-/]+)$ post.php?url=$1  
    AliasMatch ^post/([a-zA-Z-0-9-]+)/ post.php?url=$1


</VirtualHost>

在您的 .htaccess 文件中删除这两行

RewriteRule ^post/([a-zA-Z0-9-/]+)$ post.php?url=$1  
RewriteRule ^post/([a-zA-Z-0-9-]+)/ post.php?url=$1

奖金

现在,如果您想将访问旧网址 (/post?url="old_url.html") 的用户重定向到新的 seo 友好网址 (/post/old_url.html),请将其添加到您的 .htaccess 文件中

RewriteRule ^post.php?url=([a-zA-Z0-9-/]+)$ post/$1
RewriteRule ^post.php?url=([a-zA-Z-0-9-]+)/ post/$1

如果您在虚拟主机文件中将所有行作为 cmets(# infront of all lines) 将其添加到底部的虚拟主机中

 <VirtualHost *:80>
        DocumentRoot "C:/xampp/htdocs"
        AliasMatch ^post/([a-zA-Z0-9-/]+)$ post.php?url=$1  
        AliasMatch ^post/([a-zA-Z-0-9-]+)/ post.php?url=$1


    </VirtualHost>

**

请记住在进行更改后重新启动您的 Apache 服务器

**

【讨论】:

  • 修改后能否分享一下你的虚拟主机文件和htaccess文件
  • ##&lt;VirtualHost *:80&gt; ##ServerAdmin webmaster@dummy-host2.example.com ##DocumentRoot "C:/xampp/htdocs/dummy-host2.example.com" ##ServerName dummy-host2.example.com ##ErrorLog "logs/dummy-host2.example.com-error.log" ##CustomLog "logs/dummy-host2.example.com-access.log" common ##&lt;/VirtualHost&gt; AliasMatch ^post/([a-zA-Z0-9-/]+)$ post.php?url=$1 AliasMatch ^post/([a-zA-Z-0-9-]+)/ post.php?url=$1
  • Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [QSA,L]
  • 我认为这不是确切的虚拟主机文件,因为它在所有行之前都有 # 。 # 表示 cmets 。还有一件事是 AliasMatch 代码应该写在 之前
  • 这里是虚拟主机的文件目录xampp/apache/conf/extr/httpd-vhosts
猜你喜欢
  • 2013-09-17
  • 2018-10-02
  • 2018-11-27
  • 2015-08-13
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多