【问题标题】:Mod_rewrite URLs for SEO用于 SEO 的 Mod_rewrite URL
【发布时间】:2011-05-16 15:40:06
【问题描述】:

我的目录设置如下:

mywebsite/directory1/index.php
mywebsite/directory2/index.php
mywebsite/directory3/index.php
mywebsite/directory4/index.php

这对 SEO 不友好。如何使用 mod_rewrite 执行以下操作:

mywebsite/directory1/
mywebsite/directory2/
mywebsite/directory3/

如何让搜索引擎抓取我的网站而不会出现多个 index.php 的问题?

你有什么专业建议?

【问题讨论】:

    标签: apache url mod-rewrite seo


    【解决方案1】:

    将其放入您的根 .htaccess 文件中:

    RewriteEngine On
    #if your index.php is located in the root folder:
    RewriteBase /
    #or use that, if the path for your index.php is '/some/subfolder/index.php':
    #RewriteBase /some/subfolder/
    
    #rewriting only if the request file doesn't exists (you don't want to rewrite request for images or other existing files)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)? index.php?path=$1 [L]
    

    之后,您可以在根 index.php 文件中使用 REQUEST_URI:

    var_dump($_SERVER['REQUEST_URI']);
    

    或者你可以使用 $_GET['path']:

    var_dump($_GET['path']);
    

    在您的 HTML 和 CSS 中,使用图像和其他资源的绝对路径(或者最好使用完整的 URL):

    <img src="/image/image.jpg" alt="image" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      • 2020-05-06
      相关资源
      最近更新 更多