Comment out lines in multiple .htaccess files in multiple directories - bash

I need to comment out specific lines in .htaccess files for many different websites on a server.
The .htaccess files are in directories like this:
/home/siteA/public_html/.htaccess
/home/siteB/public_html/.htaccess
and so on...
these are the blocks of code I want to comment out from all .htaccess files:
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/bcrunch
<Files php.ini>
order allow,deny
deny from all
</Files>
</IfModule>
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
Ive tried the following:
root#server [/home]# shopt -s globstar
root#server [/home]# sed -i.bak -r '/<IfModule mod_suphp\.c>/,/<\/IfModule>/ s/^/#/; /<Files
"\.user\.ini">/,/<\/Files>/ s/^/#/' **/.htaccess
Ive tested it on a test directory I made with other test sub dirs and test .htaccess files in with the above content and it worked fine but once I run this from /home where I have about 30+ sites it hangs for a long time and just starts using up more and more memory. The problem is that its searching recursively and as there are other .htaccess files in other directories that Im not interested in its just getting bogged down.
So how can I change the above code and tell it to ONLY make changes to all files like this?
/home/siteA/public_html/.htaccess
and ignore other .htacccess files?
Ive been trying to use various find searches but been having no luck so far.

You can use this sed:
sed '/<IfModule mod_suphp\.c>/,/<\/IfModule>/s/^/#/; /<Files "\.user\.ini">/,/<\/Files>/s/^/#/' file
Combined with find:
find /home/site*/public_html -name '.htaccess' -exec \
sed -i.bak '/<IfModule mod_suphp\.c>/,/<\/IfModule>/s/^/#/; /<Files "\.user\.ini">/,/<\/Files>/s/^/#/' {} \;

Related

.htaccess redirect to subdirectory for Ruby application deployed by passenger

My Ruby on Rails project is on justhost server. When I am creating there ruby project, it is creating this in folder rails_apps by default.
For my project, I have created symlinks.
ln -s ~/rails_apps/webworth/public ~/public_html/webworth
And
ln -s ~/rails_apps/webworth ~/public_html/webworth_app [Purpose of this symlink just for browsing files by ftp client]
I created .htaccess file in ~/rails_apps/webworth/public directory. Below is the .htaccess code
Options -MultiViews
PassengerResolveSymlinksInDocumentRoot on
RailsEnv development
RailsBaseURI /webworth
SetEnv GEM_HOME /home5/worthgur/ruby/gems
and one .htaccess file in ~/public_html. Below is the code:
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
RewriteEngine on
Options +SymLinksIfOwnerMatch
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?webworth.com [NC]
RewriteCond %{REQUEST_URI} !webworth/ [NC]
RewriteRule ^(.*)$ "webworth\/$1" [L]
Everything is working proper with these structure of .htaccess files except one problem. Extra text is appending to all urls in my project i.e "webworth" for all links generated by routes.rb file. And I want to remove this extra text "webworth" from all URL's.
Now urls are generating for example in this manner.
http://webworth.com/webworth/tags/smartphones
while it should be http://webworth.com/tags/smartphones.
This extra text i.e "webworth" was added previously for the folder that has to be accessed since it was not in the document root for the site and because of this extra text, extra text is appending to all URL's generated by routes.rb file thru ~/rails_apps/webworth/public/.htaccess file [Reason: RailsBaseURI /webworth]
I guess this can be fixed by changing RailsBaseURI /webworth to RailsBaseURI / in ~/rails_apps/webworth/public/.htaccess file.
I did that and I am sure I could not write proper commands in ~/public_html/.htaccess file so that I can redirect the traffic from the public_html to the symlink (webworth) for my site. I used many options but couldn't figure out how it will work. Please help to write ~/public_html/.htaccess file properly to redirect the traffic from public_html folder to my symlink(webworth).
Justhost suggested this link: How to host the Primary Domain from a subfolder. But still I could not figure out the issue. Thanks
I had the same problem with justhost and I solved it without adding any redirect.
I set my rails app public folder to be the main site public folder.
I renamed the original "public_html" to "public_html_backup", just in case.
Added a symlink ln -s ~/myRailsApp/public ~/public_html
in my htaccess I changed RailsBaseURI /myRailsApp to RailsBaseURI /, resulting in:
<IfModule mod_passenger.c>
Options -MultiViews
PassengerResolveSymlinksInDocumentRoot on
RailsEnv production
RackBaseURI /
SetEnv GEM_HOME /yourUserPath.../ruby/gems
</IfModule>
I hope this can help you.
Cheers

turn off mod_rewrite for subdirectory

In httpd.conf, I have a rewrite rule in the webroot
first, I turn off mod_rewrite in the subdirectory
<directory /home/xxxx/public_html/subdir>
RewriteEngine off
</directory>
then turn on where needed
<directory /home/xxxx/public_html>
RewriteEngine On
# bunch of rewrite rules
</directory>
But rewrite engine remains on in subdir. I've done logging with RewriteLog 3 and see it processing files in subdir.
I've tried moving the subdir below the main directory section in httpd.conf but still it processes subdir files.
I know I could match and exclude the subdir in my main rules, but I'm trying to avoid the processing of that directory at all for speed.
Any ideas why or how to turn off mod_rewrite for a subdirectory?

Bash: how to check and insert a string within a block in a file

Having a block in http.conf file like:
<IfModule mime_module>
...
</IfModule>
Assuming using bash, how can I check if the following line
AddHandler application/x-httpd-php .php
has been already added within the previous block,
and, if not, how can I add it at its bottom resulting in
<IfModule mime_module>
...
AddHandler application/x-httpd-php .php
</IfModule>
This block is just in the middle of the file:
the file content doesn't start or end with this block.
EDITED:
To make this harder, I realized I need a further check too: sorry to have missed it in my question. If the above line is already present within the mentioned block as a commented line as
# AddHandler application/x-httpd-php .php
(the char # might be the first char of the line or it might have some spaced before, as it might have or not some spaces after before the char A of AddHandler), I simply need to uncomment it, otherwise I have proceed to add it as described.
awk '
/mime_module/{
flag=1
}
flag && /x-httpd-php/{
has=1
}
flag && /<\/IfModule>/{
flag = 0
if(!has)
print "AddHandler application/x-httpd-php .php"
}
1' input.conf
This might work for you:
sed -i ':a;$!N;/^<IfModule mime_module>.*<\/IfModule>/{s/# *\(AddHandler application\/x-httpd-php \.php\)/\1/;/AddHandler application\/x-httpd-php \.php/!s/.*\n/&\n AddHandler application\/x-httpd-php .php\n/;p;d};/^<IfModule mime_module>/ba;P;D' input.conf
The following will replace anything inside the block "IfModule - /IfModule" with your text "AddHandler application/x-httpd-php .php". So regardless if the comment (#) is found or if nothing is found the block will have the correct information.
Also it is IMPORTANT to note that this is an inline edit. The file you are wanting changed will be changed immediately upon running this script with NO BACKUP MADE. PRIOR TO TESTING ANYTHING PLEASE BACKUP YOUR FILE.
sed -i '/<\/IfModule>/,/<IfModule>/!s/ \.\.\..*/ \.\.\.\n AddHandler application\/x-httpd-php .php/g' /yourfile
If you are working on OS X, you may want to try the following script instead
#!/bin/bash
CR="$(printf '\r')"
sed -i '' "/<\/IfModule>/,/<IfModule>/!s/ \.\.\..*/ \.\.\.${CR} AddHandler application\/x-httpd-php .php/g" /yourfile
Example of Starting file:
<IfModule>
...
</IfModule>
<NOTHING TO CHANGE>
...
</NOTHING TO CHANGE>
<IfModule>
...
# AddHandler application/x-httpd-php .php
</IfModule>
<NO CHANGE>
...
NO CHANGE
</NO CHANGE>
Results:
<IfModule>
...
AddHandler application/x-httpd-php .php
</IfModule>
<NOTHING TO CHANGE>
...
</NOTHING TO CHANGE>
<IfModule>
...
AddHandler application/x-httpd-php .php
</IfModule>
<NO CHANGE>
...
NO CHANGE
</NO CHANGE>

Redirecting */test/* in httpd.conf to */* [Removing /test/ in path]

we've been running a test verions of our site at www.mystie.com/test/ and these urls are being used by everyday people... how can i configure httpd.conf to now redirect all these old urls to the / location?
ie,
www.mysite.com/test/image1.jpg
should be:
www.mysite.com/image1.jpg
There are other httpd conf rules in there, so i assume i 'll have to place this up front
Just add these lines:
RewriteEngine on
RewriteBase /
RewriteRule ^test/(.*)$ /$1 [R=301,NC,L]
If lines 1 and 2 have already been set, then you can omit those.
If you prefer, you can add this into your .htaccess file in the root web directory instead. Slightly slower, but saves restarting httpd.
I guess you would need a 301 redirect (which should help in SEO as well)
Redirect 301 /test/ /
Looks like #melkamo has the .htaccess covered.
The alias_module will allow you to do it in httpd.conf
`Redirect permanent /test http://localhost`
It will erase test from the URL if it is the first subdirectory

How can I get mod_rewrite to ignore certain directories and all their subdirectories?

I'm using Symphony CMS which as default has a mod_rewrite that rewrites all directories.
However, I need it to ignore the directories test and transfer and all their subdirectories.
Place a new .htacess access files in each of your two directories, and then place the following in:
mod_rewrite off
That should sort it.
Put this rule above the others:
RewriteRule ^(test|transfer)(/|$) - [L]

Resources