Forum Moderators: phranque
I have a website which consists of a main website (main domain) and several individual sites in subdirectories of the main site. I am redirecting requests for the websites that have a subdirectory in the url to their corresponding subdomains or addon domains.
What is the best way to add a trailing slash when a filename is not present, yet avoid a double trailing slash when it is?
I've tried several different things including the two following:
## Redirect a request for a subdirectory to the proper domain name.
RewriteCond %{HTTP_HOST} ^(www.)?\example\.com/anyfolder2 [NC]
RewriteRule (.*) http://www.someexample.com/$1 [R=301,L]
# Redirect a request for a subdirectory to the proper subdomain
RewriteCond %{HTTP_HOST} ^(www.)?\example\.com/anyfolder3 [NC]
RewriteRule (.*) http://www.sub3.example.com%{REQUEST_URI} [R=301,L]
It seems like there are a few options:
1. Leave the trailing slashes in the code(which causes a double slash if a filename is requested)then add lines to remove the double slashes.
2. Or should I remove the trailing slashes currently in the code (first example) and do a separate RewriteCond and RewriteRule to append a trailing slash when no filename, or blank or already present?
3. Is there a better way to do it?
I tried a few things that caused an endless loop.
Since I am using several domains and subdomains I want to keep my code as short as possible, I also don't want to do any unnecessary processing, so what is the best way to add trailing slashes or remove double slashes? I also want to have consistent www. or no www. Is including the www. the best way to go? what about for subdomains? If you think I should address these in a separate post, just let me know.
Also, what should the order be? I have two "sections" of redirects. Section 1 has redirects for the main domain main website. Section 2 (examples above) redirects old urls (example.com/somedirectories) to the proper subdomains or addon domain for those websites. I've read that more specific redirects should be first. Does that mean Section 2 should be first?
Should my order be:
check for www.
Add trailing slash (where appropriate)
Section 2
Section 1
Remove double slashes
I've read lots of posts and several library articles, but haven't got it yet.
Thank you,
KM
but if I type in
example.com/somefolder/somepage.ext
then I get this:
www.someexample.com//somepage.ext
It happens with either example. If I remove the slash in the rewrite rule after example.com/ then I don't get a double slash, but I also don't get a trailing slash, if needed.
Thank You,
KM
Here is some of my .htaccess file:
IndexIgnore *
RewriteEngine on
#
Order deny,allow
#
<Limit PUT DELETE>
Deny from all
</Limit>
AuthName example.com
ErrorDocument 404 /errors/404.php
#
#### Begin - Rewrite rules to block out some common exploits for Joomla
## these match exploits to the Query_String, then block if matched.
## I didn't include these here to conserve space
#
#The next lines are what I referred to as section 1.
#
RewriteCond %{HTTP_HOST} ^(www.)?\example\.com [NC]
RewriteCond %{HTTP_HOST} !=www.example.com
# then redirect the request to www.example.com, retaining the URL-path
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#redirect old urls to new urls for main site
RewriteRule ^filename\.php$ http://www.example.com/index.php?option=com_content&view=article&id=1&Itemid=5 [R=301,L]
RewriteRule ^somefolder(/)?$ http://www.example.com/index.php?option=com_content&view=section&id=3&Itemid=7 [R=301,L]
#
# then the ones I'm having a problem with which I called Section 2 ...
#
## Redirect a request for a subdirectory to the proper domain name.
RewriteCond %{HTTP_HOST} ^(www.)?\example\.com/anyfolder2 [NC]
RewriteRule (.*) http://www.someexample.com/$1 [R=301,L]
# Redirect a request for a subdirectory to the proper subdomain
RewriteCond %{HTTP_HOST} ^(www.)?\example\.com/anyfolder3 [NC]
#I tried the next one with request_uri, but it gave me the same double slash
RewriteRule (.*) http://www.sub3.example.com%{REQUEST_URI} [R=301,L]
# more lines below but similar to above examples, plus some 301 Redirects which I will convert to RewriteCond,RewriteRule when I get the code right.
Thank You!
KM
Put "anyfolder2" into the RewriteRule pattern instead.
RewriteCond %{HTTP_HOST} ^(www.)?\example\.com [NC]
RewriteRule ^(anyfolder2/.*)$ http://www.other-example.com/$1 [R=301,L]
The resources cited in our Apache forum Charter may prove useful to you (see links at top of this page).
Jim
[edited by: jdMorgan at 8:43 pm (utc) on Aug. 26, 2009]
So right now I have the following at the top of the .htaccess file:
IndexIgnore *
Options +FollowSymLinks
#
Order deny,allow
#
<Limit PUT DELETE>
Deny from all
</Limit>
AuthName example.com
ErrorDocument 404 /errors/404.php
RewriteEngine on
#
redirect 301 /anyfolder1/ http://www.someexample.com/
redirect 301 /anyfolder2/ http://anyfolder2.example.com/
redirect 301 /anyfolder3/ http://anyfolder3.example.com/
redirect 301 /anyfolder4/ http://www.anotherexample.com/
I've tried to replace the redirects with the following code but am getting 404 errors. The code should redirect requests for anyfolder, anyfolder/ or anyfolder/filename
RewriteCond %{HTTP_HOST} ^(www.)?\example\.com [NC]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^/anyfolder1(.*) http://www.someexample.com$1 [R=301,L]
RewriteRule ^/anyfolder2(.*) http://anyfolder2.example.com$1 [R=301,L]
RewriteRule ^/anyfolder3(.*) http://anyfolder3example.com$1 [R=301,L]
RewriteRule ^/anyfolder4(.*) http://www.anotherexample.com$1 [R=301,L]
Next in line in the .htaccess file I have the RewriteConds and RewriteRules that ARE working. These are NOT redirected to a different domain name, just different filenames or folders. The "placeholders" lines I just added so you could help me understand where the above(or correct) code should go:
# placeholder_1
RewriteCond %{HTTP_HOST} ^(www.)?\example\.com [NC]
RewriteCond %{HTTP_HOST} !=www.example.com
# placeholder_2
# then redirect the request to www.example.com, retaining the URL-path
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#redirect old urls to new urls for main site
RewriteRule ^filename\.php$ http://www.example.com/index.php?option=com_content&view=article&id=1&Itemid=5 [R=301,L]
RewriteRule ^somefolder(/)?$ http://www.example.com/index.php?option=com_content&view=section&id=3&Itemid=7 [R=301,L]
#placeholder_3
# Next are some folders that are being redirected from old urls to new urls, but still using example.com.
RewriteRule ^samples/somefolder1(.*)$ http://www.example.com/newfolder/samples/somefolder1$1 [R=301,L]
RewriteRule ^samples/somefolder2(.*)$ http://www.example.com/newfolder/samples/somefolder2$1 [R=301,L]
#placeholder4
I also have the following code which I am not sure where to put. Only part of the "Main" site uses Joomla, not the "anyfolders" I'm trying to redirect.
########## Begin - Rewrite rules to block out some common exploits
## If you experience problems on your site block out the operations listed below
## This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to set a mosConfig value through the URL
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=¦\%3D) [OR]
# Block out any script trying to base64_encode stuff to send via URL
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
# Block out any script that includes a <script> tag in URL
RewriteCond %{QUERY_STRING} (\<¦%3C).*script.*(\>¦%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL
RewriteCond %{QUERY_STRING} GLOBALS(=¦\[¦\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL
RewriteCond %{QUERY_STRING} _REQUEST(=¦\[¦\%[0-9A-Z]{0,2})
# Send all blocked request to homepage with 403 Forbidden error!
RewriteRule ^(.*)$ index.php [F,L]
#
########## End - Rewrite rules to block out some common exploits
Whew! I think I'm getting closer. Thanks for all of your help!
KM
I've done more research tonight on what is general and what is specific. I understand the concept in general and it makes sense that if a more general rule is run first, then the specific rule will not get run. But I'm still missing something. So I wasn't sure which line(s) you were referring to.
Thank you,
KM
That is, in .htaccess,
Redirect 301 [b]/an[/b]yfolder1/ http://www.example.com/
becomes
RewriteRule [b]^an[/b]yfolder1/(.*)$ http://www.example.com/$1 [R=301,L]
It is unlikely that you need the RewriteConds above these new RewriteRules, but if you do, be aware that RewriteConds affect only the single/first RewriteRule that follows them. If you do need RewriteConds to apply to multiple rules, then those RewriteConds must be reproduced for each rule, or the code logic must be modified so that one set of RewriteConds functionally applies to more than one rule -- e.g. by using the [S]kip flag or by creating and using a server environment variable (See RewriteRule [E] flag).
Jim
Okay, the light just came on about the RewriteConds. In the code further down we are clarifying what http_host should look like, but in this case, it's not needed, as we're looking at the folder named in the ReWrite Rule to redirect to the proper (new) domain name.
There are many other things you mentioned that I am still looking at and doing more research on. I will address those in another post as I know I still have a few things to clean up.
In this post, the double slash was just coming from bad code, so that was solved along the way.
Thank you!
KM