Forum Moderators: phranque
Hopefully someone can help me out. I've got a really simple mod-rewrite, but I cant get the syntax correct.
I want a RewriteRule so that:
/fixed-word/any-string-can-go-here/X
retrieves
/index.php?id=X
Is it something like...
RewriteEngine on
RewriteCond %{REQUEST_URI} /fixed-word/([a-zA-Z-]+)/?$
RewriteRule ^/fixed-word/([a-zA-Z-]+)/?$ /index.php?id=$1
RewriteEngine on
RewriteRule ^fixed-word/([a-zA-Z\-]+)/([a-zA-Z\-]+)/?$ /index.php?id=$2 [L]
It would also be better to require that either these URLs either always end with a slash or that they don't, and to use a 301 external redirect to force the issue. This will prevent you from having duplicate content at both the slashed and non-slashed URLs.
Any given page on your site should be reachable with one and only one URL, to include the domain, the URL, and any query strings. All other variants should be 301-redirected to that one canonical URL for the page.
Note that this implies that once you get the code above working, you need to add another rule so that direct client requests for /index.php?id=X are redirected back to the /fixed-word/any-string-can-go-here/X URL. This is a little tricky, so get the code above working and address that "any-string" issue in your script first.
Jim
Thanks for all that information. A big help. Reading your comments I think that my approach to create more SEF urls is flawed.
My problem is this. We are using a very simple bespoke CMS. Pages are called by /index.php?id=7. A call to a DB table is made and all the page details for ID 7 are displayed.
What I would obviously prefer would be a more SEF method where the page title from the DB is used via a RewriteRule to call the correct page.
Hope this all makes sense. Not sure where to start. Any help/guidance is greatly appreciated.
Alternately, modify your existing script to verify that "any-string-can-go-here" --which I assume is the page title-- corresponds with "X". If they don't correspond, reject the request with a 404-Not Found response to avoid duplicate-content issues and close the potential exploit.
If X is always numeric, then it will be necessary to modify the rule I posted to accept numbers:
RewriteEngine on
RewriteRule ^fixed-word/([a-zA-Z\-]+)/([0-9]+)/?$ /index.php?id=$2 [L]
Jim
My issue now is how to return the default homepage.
I've added a line of code to index.php which says if the two fields are null, return the homepage. Also, if fields do not correspond return homepage.
My problem is now that is someone request www.domain.com/// the homepage is returned. Same if someone requests www.domain.com///////// to infinite. Is this a bad thing? Any ideas on a work around?
If I was to simply return a 404 when the fields are null, then how would people access the homepage via www.domain.com ?