patternMinor
Faults using mod_rewrite
Viewed 0 times
usingfaultsmod_rewrite
Problem
I never really bothered using mod_rewrite to achieve SEO friendly names but I've always admired from a distance because I've never been good at regex.
The below .htaccess file does work, but I'm sketchy about all of it and maybe I've structured it incorrectly, furthermore it looks terrible and I know that I'm doing it wrong.
It's the very last line that has my most concerns, it sends anything other than what is matched in the first lookup to pageLoader.php and then that obviously loads the view.
Any tips, even small things would assist as this is something I want to get my head around.
The below .htaccess file does work, but I'm sketchy about all of it and maybe I've structured it incorrectly, furthermore it looks terrible and I know that I'm doing it wrong.
It's the very last line that has my most concerns, it sends anything other than what is matched in the first lookup to pageLoader.php and then that obviously loads the view.
Any tips, even small things would assist as this is something I want to get my head around.
RewriteEngine On
ReWriteRule ^css/custom.css$ PlugCMS/public/genCSS.php
ReWriteRule ^js/custom.js$ PlugCMS/public/genJS.php
ReWriteRule ^plugcms$ PlugCMS/index.php [NC]
ReWriteRule ^send-mail$ PlugCMS/public/mailhandler.php [NC]
ReWriteRule ^index\.php$ PlugCMS/public/pageLoader.php?slug=/ [NC]
ReWriteRule ^(?=^(?:(?!PlugCMS|css|js|img|robots.txt|sitemap.xml|font|fonts|sandbox|private|output).)*$)([A-Za-z0-9-]+).*$ PlugCMS/public/pageLoader.php?slug=$1 [NC,L]Solution
The thing that leaps out at me is that the normal way to avoid redirecting valid files to the pageLoader.php handler is to use
Doing it this way means that you can easily add a new file to the site without having to add to the
You usually shouldn't have to use the slug hack to get the original URL. See here for more discussion.
You may want to set a
Which would make your rewrite rules a little shorter. See here for more discussion.
RewriteCond like so: RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . PlugCMS/public/pageLoader.php [L]Doing it this way means that you can easily add a new file to the site without having to add to the
RewriteRule to avoid it matching. You usually shouldn't have to use the slug hack to get the original URL. See here for more discussion.
You may want to set a
RewriteBase for this. Perhaps RewriteBase /PlugCMS/Which would make your rewrite rules a little shorter. See here for more discussion.
Code Snippets
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . PlugCMS/public/pageLoader.php [L]RewriteBase /PlugCMS/Context
StackExchange Code Review Q#67900, answer score: 4
Revisions (0)
No revisions yet.