developerdave Posted April 26, 2010 Share Posted April 26, 2010 Hey guys, Seem to be on this particular board more and more. I have a .htaccess that has several rewrites in it that work with the CMS I've built. I've pasted it below RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule ^/?$ "http\:\/\/www\.example\.com" [R=301,L] RewriteCond %{HTTP_HOST} ^example.com$ [OR] RewriteCond %{HTTP_HOST} ^www.example.com$ RewriteRule ^index\.php$ "http\:\/\/www\.example\.com" [R=301,L] RewriteRule ^sitemap\.xml(\.gz)?$ sitemap.xml.php [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [L] From what I understand, the rule thats redirecting index.php to root is stopping all the requests (through the rewrite) to index.php. I'm really confused with this, I'm sure if can be fixed with a REQUEST_FILENAME somewhere but I'm totally unsure. Any help is greatly appeciated Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/ Share on other sites More sharing options...
developerdave Posted April 26, 2010 Author Share Posted April 26, 2010 Don't worry about it guys, given up on that decided to stick with a PHP 301 redirect instead. Code below to fix if anyone searches this. if($_SERVER['REQUEST_URI'] == '/index.php') { header ('HTTP/1.1 301 Moved Permanently'); header ('Location: http://www.seopositive.com'); } Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/#findComment-1048488 Share on other sites More sharing options...
cags Posted April 26, 2010 Share Posted April 26, 2010 RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule ^/?$ "http\:\/\/www\.example\.com" [R=301,L] This will redirect any requests to http://example.com to http://www.example.com, the L flag means that no other RewriteRules will be applied to any URL that gets rewritten by this rule. Things worth noting here are that /? is actually pointless as it means an optional slash, and the root slash is never included. Thus it could be rewritten as ^$ and have the same effect. This rule will also only redirect the one URL previously mentioned, http://example.com/anything.php will not be forwarded. Also several characters have been escaped that I don't believe are required, namely the : and the /'s and .'s. Assuming my understanding is correct I believe you would actually want this to be more like... RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,QSA] RewriteCond %{HTTP_HOST} ^example.com$ [OR] RewriteCond %{HTTP_HOST} ^www.example.com$ RewriteRule ^index\.php$ "http\:\/\/www\.example\.com" [R=301,L] Unless you have some kind of complicated set-up that uses subdomains or multiple domains on the same server (in the same directory) the URL will always be example.com or www.example.com making the two RewriteConds obsolete. You also have escaped characters you didn't need to again. I'm not entirely sure of the point of this rule to be honest since index.php is presumably the DirectoryIndex meaning requests to www.example.com will display www.example.com/index.php. This being said I assume the objective may be to remove the index.php from the URL so that it isn't visible. This being the case you probably want... RewriteRule ^index.php$ / [R=301, QSA] Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/#findComment-1048493 Share on other sites More sharing options...
developerdave Posted April 26, 2010 Author Share Posted April 26, 2010 Thanks for clearing that up Cags, you are a mod_rewrite genius. Its something I'd love to learn how to use a little more, could you recommend any good resources? Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/#findComment-1048498 Share on other sites More sharing options...
cags Posted April 26, 2010 Share Posted April 26, 2010 I'm no genius, (in mod_rewrite at least ) I just hang around these forums and try and solve other peoples issues in an attempt to improve my skills, I'd never used mod_rewrite until a few months ago. The official manual is one of the best resources once you have the basics down. Good description of the overall flow (good place to start) - http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/ Official - http://httpd.apache.org/docs/2.1/mod/mod_rewrite.html Lots of .htaccess info/tips - http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/ Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/#findComment-1048502 Share on other sites More sharing options...
developerdave Posted April 26, 2010 Author Share Posted April 26, 2010 Thanks! I'm pretty confident with PHP but never really touched on mod_rewrite so hopefully I can get a little wiser to this rather strange, but beautiful technology haha Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/#findComment-1048516 Share on other sites More sharing options...
developerdave Posted April 27, 2010 Author Share Posted April 27, 2010 Well, much to my avail. I have discovered a new issue, When I try to go to the blog it redirects to the home page. I can access www.example.com/blog but I cannot access www.example.com/blog?p=1 I've tried removing all the [L] from the .htaccess but I still can't get it to work, I had a look on some tutorial sites but its actually just baffling haha I pretty much know that L means no more rewrites to this url if this is met and QSA is query string append. But I just cannot get the blog to work again I've pasted the current .htaccess, good god I hope Cags is up this time in the morning 0.o LOL RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule ^/?$ "http\:\/\/www\.example\.com" [R=301] RewriteCond %{HTTP_HOST} ^example.com$ [OR] RewriteCond %{HTTP_HOST} ^www.example.com$ RewriteRule ^index\.php$ "http\:\/\/www\.example\.com" [R=301] RewriteRule ^sitemap\.xml(\.gz)?$ sitemap.xml.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [L] Thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/#findComment-1049144 Share on other sites More sharing options...
cags Posted April 27, 2010 Share Posted April 27, 2010 QSA is indeed query string append, you need to include this flag on any RewriteRule that you wish to keep the query string attached to (hence the fact I put it in my examples to you). RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule ^/?$ "http\:\/\/www\.example\.com" [R=301,QSA] RewriteCond %{HTTP_HOST} ^example.com$ [OR] RewriteCond %{HTTP_HOST} ^www.example.com$ RewriteRule ^index\.php$ "http\:\/\/www\.example\.com" [R=301,QSA] RewriteRule ^sitemap\.xml(\.gz)?$ sitemap.xml.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [QSA,L] I also see other changes I've mentioned haven't made it into your .htaccess, but meh. Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/#findComment-1049169 Share on other sites More sharing options...
developerdave Posted April 27, 2010 Author Share Posted April 27, 2010 I was looking at the tutorial sites before I played with it lol, boss just wants this working ASAP. I'm gonna have a play with it on my CMS at home Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/#findComment-1049173 Share on other sites More sharing options...
developerdave Posted April 27, 2010 Author Share Posted April 27, 2010 Humm, something bad happened lol. now the only thing that does work is the requests to index.php The lack of a www. doesn't redirect to www. anymore and the blog still doesn't work. Good god I wish I'd spent more time learning mod_rewrite The site is http://www.seopositive.com if you guys wanted to have a look at it and the query strings. mod_rewrite really is voodoo, very cool but a total nightmare lol I've pasted the current .htaccess below RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^seopositive.com$ RewriteRule ^/?$ "http\:\/\/www\.seopositive\.com" [R=301,QSA] RewriteCond %{HTTP_HOST} ^seopositive.com$ [OR] RewriteCond %{HTTP_HOST} ^www.seopositive.com$ RewriteRule ^index\.php$ "http\:\/\/www\.seopositive\.com" [R=301,QSA] RewriteRule ^sitemap\.xml(\.gz)?$ sitemap.xml.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [QSA,L] Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/#findComment-1049184 Share on other sites More sharing options...
developerdave Posted April 27, 2010 Author Share Posted April 27, 2010 Right, I've fixed everything but I still can't get it to exclude the blog directory and any urls inside that blog I thought this might work, but it doesnt RewriteCond %{REQUEST_URI} !^/(blog)/ before I did the rewrite request to index.php Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/#findComment-1049197 Share on other sites More sharing options...
developerdave Posted April 27, 2010 Author Share Posted April 27, 2010 I solved it by putting a .htaccess in the blog folder and telling it not to rewrite anything back to index.php on root thanks to the cheat sheet on the sticky thread Quote Link to comment https://forums.phpfreaks.com/topic/199763-mega-confused-with-my-htaccess-now/#findComment-1049347 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.