Preventing Real Directory From .htaccess Rewriting

Discuss practical ways rearrange URLs using mod_rewrite.

Preventing Real Directory From .htaccess Rewriting

Postby Wilson » Mon Nov 17, 2008 3:51 pm

I've been using the following script as my .htacess

Code: Select all
AddHandler application/x-httpd-php5 .php

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteRule \.(css|jpe?g|gif|png)$ - [L]

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?section=$1&page=$2 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?section=$1&subsection=$2&page=$3 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?section=$1&subsection=$2&page=$3&cat=$4 [QSA,L]


Somehow, since I really don't think I'll understand this mod_rewrite stuff, my site allows for the folllowing real subdirectory.

http://www.mydomain.com/forum/

In all other cases it allows for my database driven content, like

http://www.mydomain.com/user/username/

or

http://www.mydomain.com/nation/belgium/

where user and nation, of course, are not real directories.

Now, I'm wanting to add a blog to my site, which I've put at the following location:

http://www.mydomain.com/blog/


Unfortunately, the .htaccess is interpreting the blog part as a fictional directory, and as a result I'm not seeing my blog.

Where do I begin on fixing this?
Wilson
 
Posts: 11
Joined: Wed Jun 07, 2006 3:50 am

Postby Wilson » Mon Nov 17, 2008 4:15 pm

I see that if I direct my browser to...

http://www.mydomain.com/blog/index.php

...that I can at least see the blog. However, getting it to recognise the index.php is there when I direct the browser to...

http://www.mydomain.com/blog/

...is what I would hope to achieve.
Wilson
 
Posts: 11
Joined: Wed Jun 07, 2006 3:50 am

Postby richardk » Tue Nov 18, 2008 11:08 am

Try
Code: Select all
AddHandler application/x-httpd-php5 .php
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteRule \.(css|jpe?g|gif|png)$ - [L]
# Ignore the /forum and /blog directories.
RewriteRule ^(blog|forum)(/.*)?$ - [L]

RewriteCond %{SCIRPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI} [R=301,L]

RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?section=$1&page=$2 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?section=$1&subsection=$2&page=$3 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?section=$1&subsection=$2&page=$3&cat=$4 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Wilson » Wed Nov 19, 2008 1:30 am

Thanks, richardk. The blog and forum are working, but any other page seems to be giving an error:

Redirection limit for this URL exceeded. Unable to load the requested page. This may be caused by cookies that are blocked.
Wilson
 
Posts: 11
Joined: Wed Jun 07, 2006 3:50 am

Postby richardk » Wed Nov 19, 2008 7:32 am

Code: Select all
%{SCIRPT_FILENAME}

should be
Code: Select all
%{SCRIPT_FILENAME}
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Wilson » Wed Nov 19, 2008 9:05 am

I'm afraid that, even with that changed, the issue remains.
Wilson
 
Posts: 11
Joined: Wed Jun 07, 2006 3:50 am

Postby richardk » Wed Nov 19, 2008 9:30 am

Code: Select all
RewriteRule [^/]$ %{REQUEST_URI} [R=301,L]

should be
Code: Select all
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 23 guests

cron