Forum Moderators: phranque
[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?
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
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
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.
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]
Jim
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!
The URL [domain.com...]
doesn't redirect.
Should read:
The URL [domain.com...]
doesn't redirect.
RewriteRule ^DIR/(([^/]+/)*)rss\.xml$ /rssmain.xml?rsspath=$1 [L]
RewriteRule ^(([^/]+/)*)rss\.xml$ /rssmain.xml?rsspath=$1 [L]
Jim