Problem With Rewrite And No Trailing Slash On Directory

I hope dklynn sees this. I am using the rewrite rules he gave me a few years ago. :slight_smile: I use mod_rewrite to rewrite URLs on a site so the URLs can be search engine friendly (and just look nice).

I just added a PHPBB3 forum and my rewrite rules interfere with the redirect when there is no trailing slash on the directory name.

The forum is in the directory mysite.com/forum/. When someone types a URL with a directory name with no trailing slash, it is my understanding that Apache redirects the browser to the URL with the trailing slash. What happens is that my rewrite script gets invoked. I don’t want this for requests to /forum.

If I type mysite.com/forum, the rewrite rules below rewrite the request to:

mysite.com/forum/?param1=forum

I would, of course, like to get rid of the ?param1=forum. It does not seem to interfere with the PHPBB script, but I want it gone nonetheless.

This is my entire rewrite file. The first 3 lines came from dklynn and work great except for this new problem.


RewriteEngine On

RewriteRule ^/?([a-zA-Z0-9_-]+)$ /rewrite.php?param1=$1 [L]
RewriteRule ^/?([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))$ rewrite.php?param1=$1&param2=$3 [L]
RewriteRule ^/?([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))?(/([a-zA-Z0-9_-]+))?$ rewrite.php?param1=$1&param2=$3&param3=$5 [L]

# Direct all requests to www to non-www.
RewriteCond %{HTTP_HOST} ^www\\.mysite\\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]

I am wondering if there is away to exclude requests to the /forum/ directory from being subjected to the rewrite rules (except for the non-www rule). I am not sure that is the best method to accomplish this.

Any ideas?

Thank you. :slight_smile:

Why not use a rewriteCond to check for /forum first?

What you want to say is

IF the url ends with “/forum”

THEN skip the three rewrite lines (ok I dunno if other rewriting is also going on)

there’s a flag called “skip” [S=3]
if a rewriteCond matches, you can say “skip the next 3 rewriteRule lines”

Hi. No other rewriting is occurring. I included the entire rewrite file.

I tried to do a RewriteCond, but I got an internal server error. I do not know how to do this stuff and when I try to figure it out, it does not work as I think it should.

cd,

… and you didn’t check the examples in my signature’s tutorial for HOW to write an exclusion as Stomme poes suggested?

Sp,

That’s quite good, an advanced bit of mod_rewrite!

cd,

I’m back!

  1. Move your non-www code to the start (just housekeeping).

  2. Use your old RewriteRules before the NEW (PHPBB) RewriteRules. NOTE: I’ll make the assumption that you’re using Apache 2.x and clean up that ^/? bit at the start of your regex.

  3. Modify your old mod_rewrite statements to be BLOCK statements using either

RewriteCond %{REQUEST_FILENAME} !^forum/
RewriteRule ^([a-zA-Z0-9_-]+)$ /rewrite.php?param1=$1 [L]
RewriteCond %{REQUEST_FILENAME} !^forum/
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))$ rewrite.php?param1=$1&param2=$3 [L]
RewriteCond %{REQUEST_FILENAME} !^forum/
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))?(/([a-zA-Z0-9_-]+))?$ rewrite.php?param1=$1&param2=$3&param3=$5 [L]

# That's easier to get your head around as the exclusion applies to each RewriteRule
# OR
# Use the Skip flag as a GOTO directive

RewriteCond %{REQUEST_FILENAME} ^forum/
RewriteRule .? - [S=3,L]
# As Stomme poes said, this will SKIP the next three RewriteRules

RewriteRule ^([a-zA-Z0-9_-]+)$ /rewrite.php?param1=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))$ rewrite.php?param1=$1&param2=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))?(/([a-zA-Z0-9_-]+))?$ rewrite.php?param1=$1&param2=$3&param3=$5 [L]
  1. PHPBB should have created its own mod_rewrite code - IN THE forum SUBDIRECTORY - but it should NEVER be allowed to rewrite FROM an existing file/directory! When I saw your (.*) (it should be .? and use %{REQUEST_URI} in the redirection rather than /$1, i.e. RewriteRule .? http://mysite.com%{REQUEST_URI} [R=301,L]), I went NUTS thinking that it was PHPBB’s code. Just be sure that it contains the lines
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

before its (.*) redirection to index.php.

Regards,

DK

Thanks, dklynn. :slight_smile: But the problem remains. :frowning: I tried both this:


RewriteCond %{REQUEST_FILENAME} !^forum/
RewriteRule ^([a-zA-Z0-9_-]+)$ /rewrite.php?param1=$1 [L]
RewriteCond %{REQUEST_FILENAME} !^forum/
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))$ rewrite.php?param1=$1&param2=$3 [L]
RewriteCond %{REQUEST_FILENAME} !^forum/
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))?(/([a-zA-Z0-9_-]+))?$ rewrite.php?param1=$1&param2=$3&param3=$5 [L]

And this:


RewriteCond %{REQUEST_FILENAME} ^forum/
RewriteRule .? - [S=3,L]
# As Stomme poes said, this will SKIP the next three RewriteRules

RewriteRule ^([a-zA-Z0-9_-]+)$ /rewrite.php?param1=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))$ rewrite.php?param1=$1&param2=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))?(/([a-zA-Z0-9_-]+))?$ rewrite.php?param1=$1&param2=$3&param3=$5 [L]

And in both cases requests to mysite.com/forum without the trailing slash are redirected to mysite.com/forum/?param1=forum .

For whatever reason, it is not working.

Would it be possible instead of using an exclusion, which seems to be much more complex, to instead redirect all requests for mysite.com/forum (without trailing slash) to mysite.com/forum/ (with trailing slash) so it does not interfere with the www to non-www redirection?

As for the forum’s htaccess file, PHPBB did not add any redirection to its htaccess file located in the forum directory nor did it add anything anywhere else. Just in case you were wondering, I have included the entire PHPBB forum’s htaccess code following. My server is running Apache 2.2.14, by the way.


#
# Uncomment the statement below if you want to make use of
# HTTP authentication and it does not already work.
# This could be required if you are for example using PHP via Apache CGI.
#
#<IfModule mod_rewrite.c>
#RewriteEngine on
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#</IfModule>

<Files "config.php">
Order Allow,Deny
Deny from All
</Files>

<Files "common.php">
Order Allow,Deny
Deny from All
</Files>

There is really not much there.

As for the non-www redirection, I really am not sure what you are hinting at. I remember you being critical of the use of the wildcard before. So I put this in my htaccess file:


RewriteCond %{HTTP_HOST} ^www\\.mysite\\.com$ [NC]
RewriteRule .? http://mysite.com%{REQUEST_URI} [R=301,L]

It seems to work fine. Where would I put this?


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Because all that does is render the redirection of a www request to the non-www URL inactive. If it is included in the website root or included in the PHPBB forum directory, it renders the redirection from www to non-www inactive.

Adding the above to the forum directory’s htaccess file does eliminate the problem of requests to mysite.com/forum going to mysite.com/forum/?param1=forum. Requests to mysite.com/forum go to mysite.com/forum/ as they should. But it renders the redirection from www.mysite.com/forum to the non-www version inactive.

As of this moment, this is my entire rewrite code from htaccess in my website’s root.


RewriteEngine On

#Redirect www requests to non-www.
RewriteCond %{HTTP_HOST} ^www\\.mysite\\.com$ [NC]
RewriteRule .? http://mysite.com%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !^forum/
RewriteRule ^([a-zA-Z0-9_-]+)$ /rewrite.php?param1=$1 [L]
RewriteCond %{REQUEST_FILENAME} !^forum/
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))$ rewrite.php?param1=$1&param2=$3 [L]
RewriteCond %{REQUEST_FILENAME} !^forum/
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))?(/([a-zA-Z0-9_-]+))?$ rewrite.php?param1=$1&param2=$3&param3=$5 [L]

Requests to mysite.com/forum without the trailing slash continue to be rewritten to mysite.com/forum/?param1=forum .

My original hope was to put some lines in the htaccess file of my website’s root folder. But if I have to put some stuff both in the root and in the forum directory’s htaccess file, I will do that.

Otherwise, I will have to live with htaccess adding a parameter to the URL for requests to forum without the trailing slash. I don’t know if that will interfere with any else in PHPBB, though.

Thanks for your help.

cd,

Okay, I missed that you wanted to call forum without the / - so make the slash optional (or remove the slash) in the following code:

RewriteCond %{REQUEST_FILENAME} !^forum
RewriteRule ^([a-zA-Z0-9_-]+)$ /rewrite.php?param1=$1 [L]
RewriteCond %{REQUEST_FILENAME} !^forum
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))$ rewrite.php?param1=$1&param2=$3 [L]
RewriteCond %{REQUEST_FILENAME} !^forum
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))?(/([a-zA-Z0-9_-]+))?$ rewrite.php?param1=$1&param2=$3&param3=$5 [L]

And this:

RewriteCond %{REQUEST_FILENAME} ^forum
RewriteRule .? - [S=3,L]
# As Stomme poes said, this will SKIP the next three RewriteRules

RewriteRule ^([a-zA-Z0-9_-]+)$ /rewrite.php?param1=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))$ rewrite.php?param1=$1&param2=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))?(/([a-zA-Z0-9_-]+))?$ rewrite.php?param1=$1&param2=$3&param3=$5 [L]

Naw, do it right (as above).
[qote=cd]As for the forum’s htaccess file, PHPBB did not add any redirection to its htaccess file located in the forum directory nor did it add anything anywhere else. Just in case you were wondering, I have included the entire PHPBB forum’s htaccess code following. My server is running Apache 2.2.14, by the way.


#
# Uncomment the statement below if you want to make use of
# HTTP authentication and it does not already work.
# This could be required if you are for example using PHP via Apache CGI.
#
#<IfModule mod_rewrite.c>
#RewriteEngine on
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#</IfModule>

<Files "config.php">
Order Allow,Deny
Deny from All
</Files>

<Files "common.php">
Order Allow,Deny
Deny from All
</Files>

There is really not much there.
[/quote]
Well, then I was going overboard (big surprise, eh? :smiley: ) so don’t worry about it.
[qwuote=cd]As for the non-www redirection, I really am not sure what you are hinting at. I remember you being critical of the use of the wildcard before. So I put this in my htaccess file:


RewriteCond %{HTTP_HOST} ^www\\.mysite\\.com$ [NC]
RewriteRule .? http://mysite.com%{REQUEST_URI} [R=301,L]

It seems to work fine.[/quote]
Good!

I’d leave it alone is PHPBB doesn’t need it (and it only receives requests in its own directory).

Just remove the RED /'s and you should be fine.

Regards,

DK

Hey, dklynn. Thanks. I’ll try your latest suggestions out soon. :slight_smile:

Why not do it like so:


RewriteEngine On

#Redirect www requests to non-www.
RewriteCond &#37;{HTTP_HOST} ^www\\.mysite\\.com$ [NC]
RewriteRule .? http://mysite.com%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? - [S=3,L]

RewriteRule ^([a-zA-Z0-9_-]+)$ /rewrite.php?param1=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))$ rewrite.php?param1=$1&param2=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)(/([a-zA-Z0-9_-]+))?(/([a-zA-Z0-9_-]+))?$ rewrite.php?param1=$1&param2=$3&param3=$5 [L]

That way you’re not only preventing /forum from being rewritten, but all requests to existing files and directories.
So when you add a directory, you don’t to have to update the .htaccess to reflect the existence of this new directory.

Scallio,

Horray! Do you know how many YEARS it took me to see the Skip flag as a GOTO construct?

Regards,

DK