Forum Moderators: coopster
I have been working with my friend who is a developer into building this new feature where the title is modified to form the URL. For example, a page titled "What's a website?" should be made to have URL as example.com/whats-a-website
We are having problem doing this. While we are able to strip the special characters and get the URL, the page itself does not exist.
Can you see any glaring errors that could have caused this issue?
Thanks a lot.
Are you saving it to disk as an html (or some type of) file, or are you storing it in a database?
Generally, if you are storing the information in a database and creating the pages dynamically, you will need to use mod_rewrite to 'rewrite' the requested location to a php file which 'grabs' the information, either from the URL or being passed through a query_string via mod_rewrite and then access the database and serves the correct information...
IOW When you are doing this dynamically, there is usually one .php page responsible for delivering content to a large number of URLs through the use of mod_rewrite.
For example, a page titled "What's a website?" should be made to have URL as example.com/whats-a-website
I'm going to work this a little backwards, as you first need to know how you're going to hook up with that url. :-)
Check out mod rewrite as mentioned, here's a framework for doing this. Start with that, and your .htaccess.
In a lot of cases it's done like this: if it's not a directory and not a file redirect to some script.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /path-to/your-script.php [L]
This is a bit intensive and has a lot of overhead as it must search the entire server file system each time it's called. So a better solution is to use a directory name you know does not exist, and you must make sure it never exists. Let's use "articles."
Now any request for /articles you would redirect to your script.
RewriteRule ^articles/.+$ /your-script.php [NC,L]
^begins with
articles, Articles, ArTiCles, the NC makes it non case sensitive.
/Followed by a slash - a bit important because you would want a request for /articles to 404
. any character
+ one or more, in combination with above means 1 or more of any character. So if there's nothing following /articles/, it **should** give you a 404.
$ ends with
In your-script.php, you would do something like this.
// should be "domain-name.com/articles/your-article-title"
$uri = explode('/',$_SERVER{'REQUEST_URI'});
$uri_title = array_pop($uri); // "your-article-title"
$uri_category = array_pop($uri); // "articles"
In your scenario, you might not need "articles".
It's not clear if you're using static HTML files or database entries, so I'll propose both.
For static html, I would use this script to open the file and echo it out to the browser. The advantage here is the URL remains the same, no direct to the .html. So append $uri_title with '.html' and use standard file open methods to open it and just print it to the browser.
For databases, you work your logic - the one you used to get to "whats-a-website" - in reverse.
The apostrophe (and ampersands, and other special characters) are going to present some issues. The absolute simplest way to approach this is to store a page URL along with the database entry. Make sure it's unique, do a check when it's added into the DB and error if one already exists with this url.
¦id¦art_id¦title¦url¦content¦created¦lastmod
1¦2364¦What's a Web Site?¦whats-a-website¦article content¦2009-11-14¦2009-11-14
This makes life so much easier.
select art_id from articles where url='$uri_title';
If you can't do this, you will have to somehow do a search on the article titles, which can become very complex because as mentioned, you have to check for any instance of user input error when the article is created. In addition to the above quirks, this also means leading and trailing spaces, possible encoding issues on incoming URL's . . . the above route is far more predictable.
If found, pass the record id found to your output article function, otherwise, you need to generate a 404 and a message.
if ($article_id > 0) { output_article($article_id); }
else {
header("Status:404 Not Found");
output_helpful_not_found_page($uri_title);
}
The "helpful not found" is intentional. It should contain links to navigate to other articles and a contact form so they can make you aware of it.
The 404 is ALSO extremely important. Google will not verify a site that doesn't generate 404's. Using the first .htaccess example - searching the entire system - has another bad side effect: it will always redirect to the script and return a 200 OK header. To avoid this, you need to insure that a 404 header is generated for anything not found. IMO this is the largest mistake anyone can make for that scenario: redirect to main page if not found.
Once all this is working, you just need to generate the appropriate URL's on the front end as links, manually or programmatically, which it sounds like you already have.