Jump to content

Mega confused with my .htaccess now


developerdave

Recommended Posts

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

Link to comment
Share on other sites

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');
    }

Link to comment
Share on other sites

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]

 

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.