Imagine a site with just three static pages - a home page and home pages for About and Introduction sections...
(Folder About) index.php
(Folder Topics) index.php
Now imagine an .htaccess file with rewrite rules that allow me to display dynamic pages in the About and Topics sections. However, a third rewrite rule allows me to display dynamic pages on the website's home page. This is what it looks like:
RewriteRule ^About/([a-zA-Z0-9()_/-]+)/?$ About/index.php?bout=$1 [L]
RewriteRule ^Topics/([a-zA-Z0-9()_/-]+)/?$ Topics/index.php?topic=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_/]+)/?$ /index.php?area=$1 [L]
It works OK except for the section home pages (About and Topics). They work OK online, and they generally function OK locally, but the URL is http://MySite/Topics/?area=Topics instead of http://MySite/Topics
I also run into technical problems when working on my code; for example, it's harder assigning values to these pages, largely because of the weird URL's.
Is there some trick I can use to make that third rewrite rule ignore all my static pages and stop interfering with them?
The problem is that the third rule will capture most everything. So after About/whatever is rewritten to About/index.php?about=whatever, the third rule will then re-rewrite to index.php?area=About/index.php. What you'll probably need to do is attach some conditions to the third rule.
# This condition reads:
The path matched by the rule must not begin with "About" or "Topics"
RewriteCond $1 !^(?:About|Topics)(?:$|/)
You also mentioned that it's harder to assign values to these pages -- I'm guessing you mean assigning values through the query string -- and that's because when your rewritten URL contains a query string, then it replaces the existing query string. But you can fix that problem with the QSA flag, which will add any existing query string to the end of your rewritten query string.
RewriteRule ^About/([a-zA-Z0-9()_/-]+)/?$ About/index.php?bout=$1 [L,QSA]
Thanks for the tip; it works great.
This topic is now closed. New replies are no longer allowed.