Jump to content

Page names


TobesC

Recommended Posts

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
https://forums.phpfreaks.com/topic/119235-page-names/#findComment-614124
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
https://forums.phpfreaks.com/topic/119235-page-names/#findComment-614128
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
https://forums.phpfreaks.com/topic/119235-page-names/#findComment-615210
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.