Forum Moderators: phranque

Message Too Old, No Replies

Adding trailing slash to dir works in one, not another?

         

JohnKelly

4:40 pm on Mar 19, 2007 (gmt 0)

10+ Year Member



I am trying to add a 301 redirect to add a trailing slash to directory names:

[domain.com...]
... should 301 redirect to:
[domain.com...]

mod_dir is installed, and works above directory DIR. For instance:
[domain.com...] 301 redirects to [domain.com...] as it should.

In my /DIR folder I have this in the .htaccess file:
####################################################
RewriteEngine on
RewriteRule ^DIR([^.]+).*$ /contentdir/index.php?browse=$1

php_flag zlib.output_compression On
php_value zlib.output_compression_level -1
####################################################

I've tried adding the following the the above .htaccess:

RewriteCond %{REQUEST_URI}!(.*)/$
RewriteRule ^(.*)$ [domain.com...] [L,R=301]

... but can't get it to work. Either it does not add the trailing slash or it does not perform the line RewriteRule ^DIR([^.]+).*$ /contentdir/index.php?browse=$1.

Any advice please?

jdMorgan

6:28 pm on Mar 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In an .htaccess context, the path to the current directory is stripped from the variables examined by mod_rewrite. Therefore, in /DIR/, RewriteCond and RewriteRule will never see "/DIR/" in the requested path to be rewritten or redirected.

If you include "DIR" in the regular-expressions patterns, then those patterns will never match.

In order to avoid "exposing" your index.php URL, the external redirect must come first.

In /DIR/.htaccess:


RewriteEngine on
#
# Externally redirect to add a trailing slash if missing (this affects
# ALL requests not ending in a slash -- be sure that's what you want)
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.+) http://www.example.com/DIR/$1/ [R=301,L]
#
# Internally rewrite all requests to /contentdir/index.php (index.php
# must be in a different (sub)directory than this code)
RewriteRule ^([^.]+) /contentdir/index.php?browse=$1 [L]
#
php_flag zlib.output_compression On
php_value zlib.output_compression_level -1

I have stripped out superfluous regex tokens, and done a few other clean-ups as well. None should affect the function of the code, only improve its efficiency.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

JohnKelly

10:18 pm on Mar 19, 2007 (gmt 0)

10+ Year Member



Your solution worked, thanks!

It did cause a redirect that I had not anticipated however. Within the /DIR folder I have an RSS feed. A typical path might be:

[domain.com...]

rss.xml does not actually exist under that path. Rather, it is supposed to be 301 redirected to [domain.com...] and the previous path info (subdir1/subdir2/subdir3) used to generated a topical feed.

Previously I had a redirect set up like this:

###########################################
# This section needed for RSS/XML feeds
AddType application/x-httpd-php .xml
RewriteRule ^DIR([^.]+)rss.xml$ /rssmain.xml?rsspath=$1
###########################################

This was placed in the .htaccess file in the root web directory. I tried it there and in the newly modified /DIR/.htaccess file and it doesn't seem to work. Predictably, a trailing slash is added on the end of rss.xml.

Here is my /DIR/.htaccess (without the rss redirect above):

RewriteEngine on

# Externally redirect to add a trailing slash if missing (this affects ALL requests not ending in a slash -- be sure that's what you want)
RewriteCond %{REQUEST_URI}!/$
RewriteRule (.+) [domain.com...] [R=301,L]

# Internally rewrite all requests to /contentdir/index.php (index.php must be in a different (sub)directory than this code)
RewriteRule ^([^.]+) /contentdir/index.php?browse=$1 [L]

Frankly, I'm confused as to how to make both redirects (the new adding "trailing slash" fix and the rss.xml redirect) work together.

jdMorgan

11:12 pm on Mar 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One way to fix it would be:

RewriteEngine on
#
# Externally redirect to add a trailing slash if missing,
# [i]unless the requested URL has a period in it[/i]
RewriteCond %{REQUEST_URI} [b]!(\.¦/$)[/b]
RewriteRule (.+) http://www.example.com/DIR/$1/ [R=301,L]
#
# Internally rewrite all requests to /contentdir/index.php
# (index.php must be in a different (sub)directory than this code)
RewriteRule ^([^.]+) /contentdir/index.php?browse=$1 [L]

Replace the broken pipe "¦" character in the modified pattern with a solid pipe character before use; Posting on this forum modifies the pipe character.

Jim

JohnKelly

12:01 am on Mar 20, 2007 (gmt 0)

10+ Year Member



Thanks to your help I may just about have it. The trailing slash on rss.xml was resolved, but I'm still not getting the RSS rediect right.

When I put the following in my root web directory (not /DIR) .htaccess:

###########################################
# This section needed for RSS/XML feeds
AddType application/x-httpd-php .xml
RewriteRule ^DIR([^.]+)rss.xml$ /rssmain.xml?rsspath=$1
###########################################

The URL [domain.com...]
doesn't redirect.

However if I remove the / between DIR and subdir1, as below:
[domain.com...]

... the redirect to /rss_main.xml works. I found this out with a typo in my code, so I'm guessing the regex in the .htaccess isn't correct:

RewriteRule ^DIR([^.]+)rss.xml$ /rssmain.xml?rsspath=$1

So my questions are:

1) How should the RewriteRule above read? I've tried adding a / in likely places to no avail.

2) Should this above even be in the root web directory .htaccess, or rather in the /DIR/.htaccess with the other RewriteRule? It's been in the root web directory .htacess previously. If it belongs in /DIR/.htaccess, where in the file should it go (above/below other RewriteRule?)

Sorry for all the questions and I really appreciate your help. Thanks!

JohnKelly

12:05 am on Mar 20, 2007 (gmt 0)

10+ Year Member



The line in my above post:

The URL [domain.com...]
doesn't redirect.

Should read:
The URL [domain.com...]
doesn't redirect.

jdMorgan

12:21 am on Mar 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



in example.com/.htaccess

RewriteRule ^DIR/(([^/]+/)*)rss\.xml$ /rssmain.xml?rsspath=$1 [L]

- or in example.com/DIR/.htacess:

RewriteRule ^(([^/]+/)*)rss\.xml$ /rssmain.xml?rsspath=$1 [L]

If placed with the code we've discussed previously, it must go after the external redirect. It can go before or after the other internal rewrite, since the URLs make the rewrites mutually-exclusive.

Jim

JohnKelly

2:17 am on Mar 20, 2007 (gmt 0)

10+ Year Member



- or in example.com/DIR/.htacess:

RewriteRule ^(([^/]+/)*)rss\.xml$ /rssmain.xml?rsspath=$1 [L]

That's the one I used, and it works... Thanks so much!