1

I have searched and searched and tried soo many different things that just didnt work... It is beyond me. I've tried at least 50 things with no luck.. someone please help!

First I have a a file "index.php" page within the folder /blog that has a link that passes variables to a page called "post".. The MySQL in the "post.php" page pulls the MySQL data from row that matches ID and displays that specific post.

What I am trying to do is have a much cleaner and SEO friendly URL...

For example: on the "www.mydomain.com/blog/index.php" page they click a link like "post?title=This%20is%20a%20test%20title%20for%20the%20blog%20post...&id=1" which takes them to the "post.php" page in the same directory as the "index.php" page in "/blog/"..

Every single htaccess rewrite I have found is not changing the URL to a much friendlier version at all.. everything ends up staying the same..

Someone please help. I would like it to show as www.mydomain.com/blog/title rather than showing the above! If anyone could help me remove the "%" that appear in post titles for spaces and replace with "-" that would be really great too..

::desperate:: Thanks again, Adam

Currently Using this in a htaccess file located in the "blog/" directory.

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com/blog/$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/blog/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /post?title=$1&id=$2 [QSA,L]
Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57
SowersWebTech
  • 21
  • 1
  • 5

1 Answers1

0

You got a big mess in your htaccess file. A lot of that is just causing extra confusion for yourself. Start fresh.

/.htaccess

If you have an htaccess file in your root, ensure you do not have rules in there that conflict with the ones in /blog/.htaccess

/blog/.htaccess

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

## this makes /blog/whatever-whatever  actually execute /blog/post.php?title=whatever-whatever but only if the file doesn't physically exist
RewriteCond %{DOCUMENT_ROOT}/blog/$1 !-f
RewriteRule ^blog/(.*)$     /blog/post\.php?title=$1   [QSA,L]

/blog/index.php

<?php
$post_title = 'Whatever Whatever';
$post_slug = strtolower(str_replace(' ', '-', $post_title)); // replace spaces with hyphens and lower case everything
echo '<a href="/blog/'.$post_slug.'">'.$post_title.'</a>';
?>

/blog/post.php

<?php
$post_slug = $_GET['title'];

// connect to db.
// construct an SQL query such that your WHERE will have a comparison against your field but that has spaces replaced with hyphens
// for example in mysql it would be something like this
$sql = "SELECT * FROM `your_post_table` WHERE REPLACE(`your_post_title_field_column`, ' ', '--') = '".mysqli_escape_string($post_slug)."'";
// (which by the way is very crude and poses some security risks)
// learn about sql injection at http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php

// then you execute your sql query get your result and proceed as needed...
if (!empty($result)) {
    echo '<h1>'.$result['post_title'].'</h1>';
}
Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57
  • Maybe I'm thinking about things a bit backward then? I have the blog page passing the id variable from the mysql of that specific post.. and then the post page pulling the variable from the url to determine which mysql row to show. They way you're doing it would turn the link that they click to on the blog page into the "seo friendly way" and then the htaccess file would break down the url into post?whatever-whatever... Am I correct here? I was wondering why the url wouldn't change to something a little nicer. I am going to try this and let you know how it works. Thank you! – SowersWebTech Oct 26 '12 at 16:51
  • Another question for you before I start changing things around.. In response to the clutter my htaccess file.. the reasoning is 1) because I wanted to hide the .php extensions on all pages and 2) I needed to have the index.php redirect to folder because google sees 2 pages as duplicate content. It sees blog/index.php, and blog/ .. So I wanted to avoid that. If there is a better solution please let me know.. Or if I only need to have that in the root htaccess file and it will work across the board that would be great as well. I also noticed that you did not include the "id" variable... – SowersWebTech Oct 26 '12 at 17:05
  • (1) that's fine but some of those rules don't look right to me (2) and yes it is good to have all these rules in one .htaccess file in your root one and not spread out all over the place. mod_rewrite via .htaccess works backwards from inside to the root not from the root to the directories like one may think. (3) I didn't use the ID because you want friendly urls, so I assumed you don't want the ID in your url. Instead we use the seo url to look it up in the database when going from only having the friendly url. – Anthony Hatzopoulos Oct 26 '12 at 17:11
  • Anthony - Thank you so much for your prompt reply and help.. I am unable to get it working yet but I am still working on it.. In your example the post.php page uses $_GET['title'] but there is no longer a title variable in the url if we change things to you a href example on the blog/index.php page... I've changed it to post_title but still not working.. Does the title field for the post that were pulling from the db have to be all lower cases and with hypens? Currently, for example.. I was pulling from mysql db by ID.. Sample title: "Four New Menu Items To Be Excited About".. Stumped again! – SowersWebTech Oct 26 '12 at 17:38
  • `RewriteRule ^blog/(.*)$ /blog/post\.php?title=$1` makes $_GET['title'] work automagically. You'll really need to understand how mod_rewrite works. I suggest you do some research and find some other examples. – Anthony Hatzopoulos Oct 26 '12 at 17:40
  • My apologies... Thank you so much for your help. Any ideas as to why its giving me a 404? I'm thinking it cannot locate the "title" field correctly in the database.. Does capitalization matter? – SowersWebTech Oct 26 '12 at 17:46
  • I know that on the post.php page it needs to replace the hyphens with spaces to match the title entered into the database in the very least. – SowersWebTech Oct 26 '12 at 17:49
  • this is why I figured it would just be easier to pull data from the database by ID rather than title. Idk. guess Ill just go back to the old way. Thanks for your help anyway. – SowersWebTech Oct 26 '12 at 18:01
  • 404 occurs because a php file is not existing at /blog/post.php most likely. You kind of want lower case only in your urls as Apache treats /whatever and /WHATEVER as different pages. And you do not want to make the urls whatever-whatever back into spaces for the purpose of fetching from the database becasue what if you have a blog title like this: `Spider-Man Movie is a hit` the resulting friendly url would be `spider-man-movie-is-a-hit`. I hope you see where I am going with this. The other things you need to tackle are special characters like if you used the / & ! # or $ in your urls. – Anthony Hatzopoulos Oct 26 '12 at 18:03
  • No, I am with you about the lower case only in urls.. Just wondering if it's not working because it cannot find the title row because it has caps, spaces, and no hyphens... – SowersWebTech Oct 26 '12 at 18:06
  • Probably not. If your using mysql, your collation is most likely set to case-insensitive. – Anthony Hatzopoulos Oct 26 '12 at 18:08
  • Can you please who me how to include the ID so that I can just pull from db that way... easier for me. So the url on the blog page would be .. post?title=whatever-whatever&id=number and displays like blog/whatever-whatever/number ... I can figure out how to pull the row by id on the post page.. as well as the link for the blog page. – SowersWebTech Oct 26 '12 at 18:11
  • `RewriteRule ^blog/(.*)/([0-9])/?$ /blog/post\.php?title=$1&id=$2 [QSA,L]` – Anthony Hatzopoulos Oct 26 '12 at 18:31
  • Anthony you have been very helpful! It is still not working for some reason... Not sure why.. keeps sending me to my 404 page.. Even though the post.php page does infact exist. blah. – SowersWebTech Oct 26 '12 at 18:35
  • where did you put that code? /.httaccess or /blog/.htaccess You're going to have to do a little work yourself, I can't do everything for you. – Anthony Hatzopoulos Oct 26 '12 at 18:38
  • Works perfectly when I test it. – Anthony Hatzopoulos Oct 26 '12 at 18:45
  • have this as my /blog/.htaccess ... RewriteCond %{DOCUMENT_ROOT}/blog/$1/$2 !-f RewriteRule ^blog/(.*)/([0-9])?$ /blog/post\.php?title=$1&id=$2 [QSA,L] And for some reason still goes to 404 when I click the link on the index.php page. Which is going to a custom 404 but Ive deleted that line out of every htaccess file so not sure how its telling it to go there. fml – SowersWebTech Oct 26 '12 at 19:10