Jump to content

Page names


TobesC

Recommended Posts

Hey everyone - does anyone know what kind of code to write so that a page is created with the title of the article so that it can get crawled by Google?

 

For example, when I read an article on wordpress the page comes up as ../this-is-the-article/.

 

Thanks.

Link to comment
Share on other sites

Generally, you use what is called a rewrite rule, with is done through the mod_rewrite extension in Apache. Alternatively, you can use custom 404 pages - I'm sure there are other methods for non-Apache setups.

 

http://www.flashgamereviews.com/Bloons-Tower-Defense-2-Review.html

 

Done by...

 

RewriteEngine On

RewriteRule ^([A-Za-z0-9\!&-])+\.html$ index.php

 

in the .htaccess file

 

Little word of advice, these are HARD to write. There a bitch to debug, act funny when you chain them, and require regex which you don't know.

Link to comment
Share on other sites

I think you may be asking about Search Engine Friendly (SEF) URLs in Wordpress (which the folks at Wordpress call "permalinks").

 

I'm not sure when the feature appeared, but you can specify the permalinks for each article (see attachment)

 

[attachment deleted by admin]

Link to comment
Share on other sites

I'm not sure if this is the only solution, but this is how I always see it done . . .

 

in you .htaccess put:

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

 

This will redirect requests to everything other than media files (files with extensions: js, ico, gif, jpg, png, and css) to index.php, internally. In index.php, you would then parse the $_SERVER['REQUEST_URI'] variable and run the appropriate code.

 

A sample request:

1. User opens browser to http://www.domain.com/posts/how-to-use-modrewrite/

2. Apache (specifically, mod_rewrite) notices that this is not a media request, so it runs index.php. This does _not_ perform an HTTP redirect.

3. index.php parses $_SERVER['REQUEST_URI'] and runs some code to generate some output, which is then sent back to the user.

 

This process of parsing an HTTP request, and deciding which code to run is commonly called the Router design pattern. The index.php file could be said to use the Front Controller design pattern.

 

Here's a very simplistic example of what your index.php file might look like:

<?php

// Assume the user requested: http://www.domain.com/posts/how-to-use-modrewrite/

if ( 0 === strpos( $_SERVER['REQUEST_URI'], '/posts/' ) ) {
   // User wants to do something with blog posts, so instantiate the Blog class
   $blog = new Blog();
   $title = substr( $_SERVER['REQUEST_URI'], 7 ); // Strip out the title; 7 is the position in the string that the title starts
   $content = $blog->getContentByTitle( $title );
   include 'postTemplate.phtml'; // This file would consist of mostly HTML, but would also echo $title and $content at some point
// closing php tag is not necessary here and should be left out, but that's a whole other topic

 

This is untested code, but you should get the basic idea.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.