Reputation: 85
I have a CMS that handles SEO friendly URLs created with the CMS. So anything that is an extension of index.php
like www.url.com/index.php?id=slug
is nicely changed to www.url.com/slug
.
However, I also have over a thousand pages that are created with MYSQL query strings that are not connected to my CMS. So I have:
www.url.com/item.php?id=001234&pg=author-title
the information in id and pg are required to produce the page. What I want are for these page urls to be:
www.url.com/item/001234/author-title.html
I've looked at a number of mod_rewrite tutorials searching for all the ways to do this but I can't seem to get it to work.
Here is my .htaccess (keep in mind some of the things in here are used and generated by my CMS:
Edit: Okay, based on the answers so far I have changed my .htaccess to the following:
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
AddDefaultCharset UTF-8
# File modified on Dec 5 11:28:31 2011 by server
# For security reasons, mod_php is not used on this server. Use a php.ini file for php directives
# php_value default_charset "UTF-8"
Options -Indexes
Options +FollowSymLinks
# blocks direct access to the XML files - they hold all the data!
<Files ~ "\.xml$">
Order allow,deny
Deny from all
Satisfy All
</Files>
<Files sitemap.xml>
Order allow,deny
Allow from all
Satisfy All
</Files>
RewriteEngine on
# Usually it RewriteBase is just '/', but
# replace it with your subdirectory path
RewriteBase /
# Usually it RewriteBase is just '/', but
# replace it with your subdirectory path
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?item/([0-9]+)/([a-zA-Z0-9_]+).html$ item.php?id=$1&pg=$2 [QSA,L]
Edit: After changing the .htaccess with the feedback so far, the rewritten URLs are still not producing the desired results. I'm still getting a 404.
Upvotes: 2
Views: 1406
Reputation: 3028
Change
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L] #passes to CMS
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+).html$ item.php?id=$1&pg=$2 [QSA,L] #passes to your stuff
to
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?item/([a-zA-Z_]+)/([a-zA-Z_]+).html$ item.php?id=$1&pg=$2 [QSA,L]
# conditions need to be repeated
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
Always put more specific rules above more general ones.
And unless your item IDs use letters use:
RewriteRule ^/?item/([0-9]+)/([a-zA-Z_]+).html$ item.php?id=$1&pg=$2 [QSA,L]
Upvotes: 1
Reputation: 2923
You need to add item
at the start to match the example you give and make some changes to your character classes. Try:
RewriteRule ^item/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)\.html$ item.php?id=$1&pg=$2 [QSA,L]
This will match a URL of the form item/[string of letters, numbers and underscore]/[string of letters, numbers and underscore].html
If your IDs are only numeric you can remove the letters from the first character class.
Upvotes: 0