jdubwelch Posted July 24, 2010 Share Posted July 24, 2010 For this project I'm working on, I need to create friendly URLs using mod_rewrite. This is what I've generated to create the create links: RewriteRule ^NCAA/Pac-10/Oregon-Ducks/$ index.php?teamID=1 RewriteRule ^NCAA/Pac-10/Oregon-Ducks/page/([0-9]+)/ index.php?teamID=1&page=$1 RewriteRule ^NCAA/Pac-10/Oregon-Ducks/category/([0-9])/.*/page/([0-9])/ categories.php?teamID=1&catID=$1&page=$2 RewriteRule ^NCAA/Pac-10/Oregon-Ducks/category/([0-9]+)/ categories.php?teamID=1&catID=$1 RewriteRule ^NCAA/Pac-10/Oregon-Ducks/source/([0-9]+)/.*/page/([0-9])/ sources.php?teamID=1&sourceID=$1&page=$2 RewriteRule ^NCAA/Pac-10/Oregon-Ducks/source/([0-9]+)/ sources.php?teamID=1&sourceID=$1 RewriteRule ^NCAA/Pac-10/Oregon-Ducks/date/([0-9]{4}\-[0-9]{2}\-[0-9]{2})/.*/page/([0-9])/ date.php?teamID=1&date=$1&page=$2 RewriteRule ^NCAA/Pac-10/Oregon-Ducks/date/([0-9]{4}\-[0-9]{2}\-[0-9]{2})/ date.php?teamID=1&date=$1 RewriteRule ^NCAA/Pac-10/Oregon-Ducks/popular/.*/page/([0-9])/ popular.php?teamID=1&page=$2 RewriteRule ^NCAA/Pac-10/Oregon-Ducks/popular/ popular.php?teamID=1 RewriteRule ^NCAA/Pac-10/Oregon-Ducks/contribute/ contribute.php?teamID=1 RewriteRule ^NCAA/Pac-10/Oregon-Ducks/([0-9]+)/ headline.php?teamID=1&id=$1 There's 65 teams total though. That means I'd have to duplicate this code and have it in my .htaccess file 65 other times. The URL structure is based on team info determined by the teamID. Is there a better way to do this? With less code maybe? i read this article at sitepoint about the mapping feature of mod_rewrite (http://articles.sitepoint.com/print/guide-url-rewriting... that would essentially be what I want to do (a static file stores the ids and values as a lookup), but with my hosting company I don't have access to modifiy the httpd.conf file. Quote Link to comment https://forums.phpfreaks.com/topic/208793-friendly-urls-fasterbetter-way-for-many-rewrites/ Share on other sites More sharing options...
cags Posted July 25, 2010 Share Posted July 25, 2010 I have no idea about the NCAA or Pac-10, but I'm going to go ahead and assume they are an organisation and a division. Without knowing more it's difficult to be any more accurate, but I'll assume the 'third directory' is the team name and that is the only other variable. RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)/$ index.php?teamID=$1 RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)/page/([0-9]+)/ index.php?teamID=$1&page=$2 RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)category/([0-9])/.*/page/([0-9])/ categories.php?teamID=$1&catID=$2&page=$3 RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)/category/([0-9]+)/ categories.php?teamID=$1&catID=$2 RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)/source/([0-9]+)/.*/page/([0-9])/ sources.php?teamID=$1&sourceID=$2&page=$3 RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)/source/([0-9]+)/ sources.php?teamID=$1&sourceID=$2 RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)/date/([0-9]{4}\-[0-9]{2}\-[0-9]{2})/.*/page/([0-9])/ date.php?teamID=$1&date=$2&page=$3 RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)/date/([0-9]{4}\-[0-9]{2}\-[0-9]{2})/ date.php?teamID=$1&date=$2 RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)/popular/.*/page/([0-9])/ popular.php?teamID=$1&page=$2 RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)/popular/ popular.php?teamID=$1 RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)/contribute/ contribute.php?teamID=$1 RewriteRule ^NCAA/Pac-10/([a-zA-Z-]+)/([0-9]+)/ headline.php?teamID=$1&id=$2 This of course means that you will now need to change your SQL query to fetch the information about the team from the database based on 'permalink-name' rather than ID. Quote Link to comment https://forums.phpfreaks.com/topic/208793-friendly-urls-fasterbetter-way-for-many-rewrites/#findComment-1090851 Share on other sites More sharing options...
jdubwelch Posted July 25, 2010 Author Share Posted July 25, 2010 So here are more examples of teams (these are just for the index page not all the other pages though): RewriteRule ^NCAA/Big-12/Colorado-Buffaloes/$ index.php?teamID=12 RewriteRule ^NCAA/Big-12/Texas-Longhorns/$ index.php?teamID=20 RewriteRule ^NCAA/Big-10/Ohio-State-Buckeyes/$ index.php?teamID=30 RewriteRule ^NCAA/SEC/Florida-Gators/$ index.php?teamID=37 The only variable I need is the teamID to make the page display correctly. However, I want the url structure to be based on that team info. Which is <sport>/<conference>/<team>/ So rather that /index.php?teamID=12... I want the sport/conference/team/. Quote Link to comment https://forums.phpfreaks.com/topic/208793-friendly-urls-fasterbetter-way-for-many-rewrites/#findComment-1090932 Share on other sites More sharing options...
cags Posted July 25, 2010 Share Posted July 25, 2010 Well then the example I gave before will work, with the exception that you will need to make a regex to match the 'second directory'. E.g. RewriteRule ^NCAA/[a-zA-Z0-9-]+/Colorado-Buffaloes/$ index.php?teamID=12 Quote Link to comment https://forums.phpfreaks.com/topic/208793-friendly-urls-fasterbetter-way-for-many-rewrites/#findComment-1090934 Share on other sites More sharing options...
jdubwelch Posted July 25, 2010 Author Share Posted July 25, 2010 I guess I'm still confused. With your method, how would I tell the script what teamID it is? Here's a link to all rewrites i will need..... http://oneclicksportsnow.com/tmp/htaccess.php to do. I'm not sure if i can just do a regex for the whole url... because i've got to include the teamID for each one. I noticed with yours you have teamID=$1, but how will that send the teamID to the script? Quote Link to comment https://forums.phpfreaks.com/topic/208793-friendly-urls-fasterbetter-way-for-many-rewrites/#findComment-1090985 Share on other sites More sharing options...
cags Posted July 25, 2010 Share Posted July 25, 2010 I already explained in my first post. It's impossible to convert 'Oregon-Ducks' to 1 dynamically with .htaccess, but you can capture the 'Oregon-Ducks' part of the URL and look that up in your database or whatever rather than looking up 1. i.e. it will be a permalink slug value. Quote Link to comment https://forums.phpfreaks.com/topic/208793-friendly-urls-fasterbetter-way-for-many-rewrites/#findComment-1090992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.