I have a website where people can post videos and images.All posts have link format like this: domain.com/p/7 and I want to know how to have title of post instead a number of post.For example: domain.com/p/post-title. I think is need to change that line from .htacces but I don't know what exactly what to change: RewriteRule ^p/(.*) view.php?pid=$1
Asked
Active
Viewed 60 times
0
-
1What have you tried? You will always receive better results if you post your code. – Mar 24 '13 at 19:55
-
I don't try because I don't know what to change on code... – Oasis Cosmin Mar 24 '13 at 19:56
-
Nevertheless you should post the code so that we can see whats wrong with it. – Mar 24 '13 at 19:57
-
This doesn't look like something you want to fix with `.htaccess`. When you generate (concat) the URL, why not just sanitize your topic title and store that separately in your database (or file) so you can access it and prevent duplicates. `$_SERVER['HTTP_HOST'].'/p/'.$sanitized_title`. For example; in MySQL store an extra field `url_refference (TEXT)`. – Mar 24 '13 at 20:00
-
More info on URL sanitation here: http://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe. In your case you would only do this method on your post title. – Mar 24 '13 at 20:07
1 Answers
1
You will need to change more than that. First, you want to move from ID to post-title (a url safe alias I presume). So the mod_rewrite will be something like
RewriteRule ^p/(.*) view.php?title=$1
But now you have to change view.php. Where you are selecting the post by ID right now, you change it to select by post-title/alias. And maybe this needs to be changed in other places also. Maybe its easier to change domain.com/p/7 to domain.com/p/7/post-title and then change the rewrite to:
RewriteRule ^p/[0-9]+/(.*) view.php?pid=$1
That way you won't have to change view.php

Omnisite
- 287
- 1
- 7
-
How would `.htaccess` know the id/title relation? The idea is to retrieve by the clean URL. This way will still rely on an "ugly" url as a saved variant. – Mar 24 '13 at 20:03
-
In my first rewrite, that title is catched. Thats the cleanest URL. Htaccess does not need to know the id/title relation. You do that in view.php. So you need to change view.php as I wrote, but if the topic starter does not know PHP, he's better of with my second answer (domain.com/p/7/post-title) – Omnisite Mar 24 '13 at 20:14