7.3. Passing URL Values to admin.php with .htaccess

As your application stands right now, the URLs in your administrative links won't mean anything to admin.php. To remedy this, you need to modify .htaccess with an additional rule that handles URLs passed in a link to admin.php.

7.3.1. Modifying the Original Admin Rule

When you write this rule, you need to keep in mind that new entries are passed to admin.php without a URL, so you need to allow for scenarios where a URL is passed to admin.php, as well as for scenarios where it isn't. To do this, you'll modify your original admin rule to ensure that the path ends with the page name, followed by either a forward slash or nothing at all. With these modifications, your rule in .htaccess should look like this:

RewriteRule ^admin/(w+)(|/)$ admin.php?page=$1 [NC,L]

You modify this rule in your addition of (|/)$, which tells the server to ensure that the end of the path must be encountered after one or more word characters, whether that occurs at the end of the word characters or after one occurrence of a forward slash.

The (|/) tells the server to match either nothing or a forward slash. The vertical pipe character (|) is the regular expression equivalent of "or".

Adding a dollar sign ($) to the end of the rule lets you signify the end of the string, so nothing can come after the pattern you define.

Thus, both of the following examples match your new rule:

http://localhost/simple_blog/admin/blog
http://localhost/simple_blog/admin/blog/

However, this example does not match your rule:

http://localhost/simple_blog/admin/blog/entry

7.3.2. The New Admin Rule

The next step is to set up a rule that catches information in the URL after the page and passes it to admin.php as a URL variable; this enables you to signify which entry is being edited. Accomplishing this requires that you add an additional backreference for the URL of the entry you want to edit. This backreference needs to catch the entire URL, so the word character shorthand (w) won't be enough, since your URLs contain hyphens. To add hyphens as a matchable character, you'll have to create a character class using square brackets.

NOTE

Backreferences are named matches that you can use in the replacement. For a refresher on backreferences, see the section on .htaccess in Chapter 6.

You pass the first backreference in the URL query string as a page, just like your original rule. You pass the second backreference as a URL, to let admin.php know which entry is being edited.

To implement this rule in .htaccess, add the bold line to your .htaccess file:

RewriteEngine on
RewriteBase /simple_blog/

RewriteRule .(gif|jpg|png|css|ico|swf|js|inc.php)$ - [L]
RewriteRule ^admin/(w+)(|/)$ admin.php?page=$1 [NC,L]
RewriteRule ^admin/(w+)/([w-]+) admin.php?page=$1&url=$2 [NC,L]
RewriteRule ^(w+)(|/)$ index.php?page=$1
RewriteRule ^(w+)/([w-]+) index.php?page=$1&url=$2

Your second backreference, ([w-]+), will match one or more word characters and/or hyphens—which is what your custom entry URLs consist of—and pass their value to admin.php. Now you're ready to modify admin.php to load entries for editing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.5