簡體   English   中英

.htaccess php 友好的 Urls SEO

[英].htaccess php friendly Urls SEO

不確定我的 .htaccess 文件有什么問題,或者我可能還不明白如何使用它。

據我了解:

*我可以在 <a> 標簽中放置 href=" " 之類的東西

    someMeaningfulStuff/post-about-something-here

*我可以在與 php 腳本相同的目錄中有一個 .htaccess 文件,該文件是鏈接的實際目標,可以獲取所述 url (rewriteRule),使用正則表達式提取其中的一部分

將其插入到資源所在的實際 url 中,而客戶端在其瀏覽器 url 地址上具有類似以下內容:

    http://localhost:8080/adminBlog/post-about-something-here

盡管它實際上在這里:

    http://localhost:8080/adminBlog/viewPost.php?id=1

我錯了嗎? 這是我的文件thanx提前!

  1. index.php(root) 中的 url 到http://localhost:8080/adminBlog/viewPost.php?id=1

     echo '<h3><a href="/adminBlog/' . $res["postUrl"] . ' " target="_blank" title="link to post" rel="help">' . $res["postTitle"] . '</a></h3>';

其中 $res['posturl'] 是從查詢到數據庫的響應,該數據庫包含以下形式的響應:

    some-title-to-some-blog-post
  1. 有 wamp/apache 配置:

     LoadModule rewrite_module modules/mod_rewrite.so

     AllowOverride All Options Indexes FollowSymLinks MultiViews Order allow,deny allow from all

  2. .htaccess 文件:

     Options FollowSymLinks RewriteEngine on RewriteBase /adminBlog RewriteRule ^/?adminBlog/(.+)$ /viewPost.php?id=$1

5.在http://localhost:8080/adminBlog/viewPost.php

$q = $dbConPDO->prepare('SELECT postId, postTitle, postDesc, postDate, postCont FROM ' .$x. ' WHERE postUrl = :friendlyUrl');
$q->execute(array(':friendlyUrl' => $_GET['id']));
$res = $q->fetch();

6.目錄

    -c
      -wamp
        -www
          -index.php
          -adminBlog //(this dir is within www as index.php is)
            -.htaccess //(as above in #3)
            -viewpost.php

如果你想要http://localhost:8080/post-about-something-here ,.htaccess 文件應該在根文件夾中。 否則,它將不起作用。

解決了!

我錯誤地推斷 .htaccess 文件必須位於目標 php 腳本的文件夾中,該腳本用於處理數據,而引用位於根文件夾中。

1. 根 (/) 中的 Referrer (<a href='' >;, in index.php) 現在顯示為:

    echo '<h3><a href="viewPost/' . $res["postUrl"] . '" target="_blank" title="link to post" rel="help">' . $res["postTitle"] . '</a></h3>';

2 .htaccess 在 root 現在顯示:

 RewriteEngine on
 RewriteBase /
 RewriteCond %{HTTP_HOST} .+
 RewriteRule ^/?viewPost/(.+)/?$ adminBlog/viewPost.php?id=$1

也許 RewriteBase 是不必要的,因為該文件位於根目錄中。

就這么簡單!

感謝您的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM