htaccess - not allowing /blog to work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

htaccess - not allowing /blog to work

Post by simonmlewis »

We are trying to use this code to allow the Blog to have it's own URL in the site, but while preventing dodgy URLs too.

Why does this reroute "/blog" to "/blog/?page=blog".

Would this code cause that?

Code: Select all

RewriteRule ^(blog)($|/) - [QSA]
# Remove multiple slashes after domain
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]

# Remove multiple slashes anywhere in URL
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]


Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: htaccess - not allowing /blog to work

Post by Celauran »

What's the incoming request? I don't see anything there that would rewrite /blog to /blog/?page=blog but the QSA on the first rule would maintain the ?page=blog if it already existed.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: htaccess - not allowing /blog to work

Post by simonmlewis »

This is it from the top.

Oddly it's the same as one of our UK sites, and that has no issue.

Code: Select all

DirectoryIndex index.php index.html index.htm
order allow,deny
allow from all 

Options +FollowSymLinks
Options +Indexes
RewriteEngine On

ErrorDocument 404 /custom_404.php
# RewriteCond %{HTTPS} !=on
# RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^(blog)($|/) - [QSA]
# Remove multiple slashes after domain
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]

# Remove multiple slashes anywhere in URL
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: htaccess - not allowing /blog to work

Post by Celauran »

The question about the incoming request remains, though. What are you trying to rewrite?

[text]RewriteRule ^(blog)($|/) - [QSA][/text]
Doesn't really do anything.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: htaccess - not allowing /blog to work

Post by simonmlewis »

It's so that if someone goes to www.website.co.uk/blog, it goes to the blog folder where it has Wordpress's structure.
We use this on all our websites and this code makes that work. Or it did!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: htaccess - not allowing /blog to work

Post by Celauran »

simonmlewis wrote:It's so that if someone goes to http://www.website.co.uk/blog, it goes to the blog folder where it has Wordpress's structure.
WordPress. That's maybe relevant. Are you sure that's not where the ?page=blog is coming from? WordPress has its own set of rewrite rules.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: htaccess - not allowing /blog to work

Post by simonmlewis »

Absolutely certain. Because this is BEFORE I have even had a chance to get to run the Install for Wordpress.
normally you upload the files. go to /blog, then you see the form to enter the database credentials.

I'm not even getting to that stage!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: htaccess - not allowing /blog to work

Post by Celauran »

There's nothing in your htaccess above that would be adding ?page=blog. The QSA on the blog rule would keep it, but it would have to already be there.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: htaccess - not allowing /blog to work

Post by simonmlewis »

Sorry I don't understand.
So you are saying there is nothing above that rule that would hinder it working.
We have PHP scripts that do stuff, like set URLs to be lowercase, and redirect if a product is not found, but nothing that does that. the other site we have that has very similar code indeed, works a treat with the same extra PHP files.

I just don't get it. When you first upload Wordpres, it doesn't install a HTACCESS does it. OR shoudl it?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: htaccess - not allowing /blog to work

Post by simonmlewis »

Well I'll be damned. It needed the extra HTACCESS before even running.

I just pinched this from another site, and uploaded it and it now works (ready to run the install).

Code: Select all

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply