简体   繁体   中英

.htaccess php friendly Urls SEO

Not sure what is the problem with my .htaccess file or perhaps I do not understand yet how to use it.

As I understand it:

*i can place in href=" " in <a> tag something like

    someMeaningfulStuff/post-about-something-here

*i can have an .htaccess file in the same directory as the php script that is the actual target of the link which can pick up said url (rewriteRule), use a regular expression to extract a portion of it

AND

insert it in the actual url where the resource lives while client has on their browser url addres something like:

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

eventhough it is actually here:

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

Am I wrong? Here is my files thanx in advance!

  1. the url in index.php(root) to 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>';

where $res['posturl'] is the response from a query to a database which holds a response in the form:

    some-title-to-some-blog-post
  1. have wamp/apache config:

     LoadModule rewrite_module modules/mod_rewrite.so

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

  2. .htaccess file:

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

5. in 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. directory

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

If you want http://localhost:8080/post-about-something-here , the .htaccess file should be at the root folder. otherwise, it won't work.

Solved!

I was mistakenly reasoning that the .htaccess file had to be in the folder of the target php script that was to handle the data while the referrer was in the root folder.

1. Referrer (<a href='' >;, in index.php) in root (/) now reads:

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

2 .htaccess in root now reads:

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

Perhaps the RewriteBase is unnecessary as this file is in root dir.

It was as simple as that!

Thanx for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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