How to edit .htaccess Server-wide - apache

I use WHM for hosting which allows me to make many cpanel accounts. Every time I make a cpanel account it has a new .htaccess file inside public_html with the same code inside
I want to be able to change this code to mine but not on one cpanel but rather on all. so every new cPanel has my edited code automatically when made rather then editing it manually.
I cant seem to find the default .htaccess on my server. How can I go about doing this?
I use litespeed also.
tried to find the .htaccess config on server and inside whm

I think there is such thing as a "global htaccess"
for WHM/cPanel, the closest thing you can do is Include that can affect all vhost
not sure what you want to do with htaccess, but I assume it should also work in Apache conf file
e.g.
/etc/apache2/conf.d/userdata/ssl/2_4/deny.conf
/etc/apache2/conf.d/userdata/std/2_4/deny.conf
put content like
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} xmlrpc.php
RewriteRule .* - [F,L]
</IfModule>
restart Apache/LSWS, then this will make all vhost's /xmlrpc.php end up in 403 error.
ref: https://docs.cpanel.net/ea4/apache/modify-apache-virtual-hosts-with-include-files/

Related

Remove cPanel configuration paths in shared hosting

I have a severe problem, my cPanel URLs public, I don't want anyone to know the cPanel configuration URLs because if any user can access it with yourdomain.com/cpanel.
As I have shared hosting, I don't have access to the httpd/root or the server configuration files. I want to know whether I can add some code to the .htaccess file and stop this redirection.
I previously had shared hosting, and I discovered that this is not possible in shared hosting, you need to have root access.
I bought a vps hosting and removed it by doing the following:
Copying the Apache 2.4 template for EasyApache 4 to allow for customization using command line/terminal:
cp -a /var/cpanel/templates/apache2_4/ea4_main.default /var/cpanel/templates/apache2_4/ea4_main.local
By editing /var/cpanel/templates/apache2_4/ea4_main.local to change the entries to match your preferences:
vim /var/cpanel/templates/apache2_4/ea4_main.local
For instance, if you wanted to disable the /cpanel alias, you'd remove this line when editing the file:
ScriptAliasMatch ^/?cpanel/?$ /usr/local/cpanel/cgi-sys/redirect.cgi
And then rebuilding the httpd.conf file by using:
/scripts/rebuildhttpdconf
And the last step is to restart by using:
service httpd restart
And your cPanel conf paths will be removed.
If you want to deny access to http://www.example.com/cpanel, do this:
In httpd.conf make sure you load mod_rewrite: LoadModule rewrite_module modules/mod_rewrite.so. Since you are on a shared hosting, you may not have access to that, but then it is most probably already loaded.
In your .htaccess, add:
RewriteEngine On
RewriteCond %{QUERY_STRING} "^/cpanel$"
RewriteRule ".*" "-" [F,L]
Tag [F] causes the server to return a 403 Forbidden status code to the client (ref: https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_f)
To ensure the .htaccess directives are taken into account, make sure you add this to the options of the directory where it resides:
AllowOverwride All
Refer to this SO question: How to Set AllowOverride all

How can I have redirect or make an alias for my websites directories?

Real directory structure:
http://example.com/directory1/directory2/directory3/hiddendirectory/directory4/
I need the file to open as:
http://example.com/hiddendirectory/directory4/
in the browser, but in reality the directory still be in the first location.
I am on shared hosting with hostgator and do not have full privileges so I cannot run any scripts.
I've done some research and I've messed around trying create an alias, but I can't get anything to work.
Here is what I put in the .htaccess file:
Alias /hiddendirectory/directory4 /directory1/directory2/directory3/hiddendirectory/directory4
But I always get a 500 server error. Any advice would be appreciated.
Alias directive runs on the Server config context not in htaccess.
In htaccess you can use mod_rewrite to rewrite your dirs.
RewriteEngine On
RewriteRule ^hiddendir/dir4/?$ /dir1/dir2/dir3/hiddendir/dir4 [NC,L]
This will rewrite
/hiddendir/dir4/
to
/dir1/dir2/dir3/hiddendir/dir4

htaccess issue with seo friendly urls - GoDaddy

OK, I'm looking to use mod rewrite to write seo friendly urls for my site. It works fine on my xampp local testing server. However, I'm having problems when trying to put it live, it doesn't seem to work at all.
Here's what I've written in my htaccess file:
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9]+)$ category.php?location=$1
So the idea is to change mysite.com/category.php?location=londonto mysite.com/category/london.
Initially I though it may be an issue with the ability to mod rewrite being switched on on GoDaddy's end, so I contacted GoDaddy to see if it that was the case and their response was that: "Mod_rewrite is an Apache Web server module installed on all of our Linux servers by default; it does not have to be installed or enabled. Our Linux hosting accounts support most mod_rewrite functions. Because enabling mod_rewrite is handled at a global level, you do not need to enable it in your httpd.conf file. You only need to add the desired code to the body of your .htaccess file."
And they bascially implied that the error is not with them and with my code. I'm not very experienced with mod rewrites and it seemed to work fine when I wrote it like this on other servers. Is there something special you have to write with godaddy? Am I missing something? Thanks!
Is that all in your htaccess file? What error do you get when visiting a desired URL, is it a 500 Internal Server error?
What I usualy add by default is an If-statement:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^category/([a-zA-Z0-9_-]+)$ category.php?location=$1 [L]
</IfModule>
By the way, note the _- at the end of the charset in the rule, for instance New York has a SEF version which is new-york, category/new-york would fail in your RewriteRule.

rewritemap for SEO and pretty URLs

I am attempting to redirect & rewrite some dynamic PHP URL's to pretty and SEO friendly URLs. I have manged to do this successfully through .htaccess with the following code:
RewriteCond %{QUERY_STRING} ^somevar=green&nodescription=([a-zA-Z0-9_-]*)$
RewriteRule (.*) /green\/%1\/? [L,R=301]
RewriteRule ^green/([^/]*)/$ /script.php?somevar=green&nodescription=$1&rewrite=on [L]
This creates a somewhat pretty URL as follows:
http://www.mysite.com/green/aA43-/
As I say, this works absolutley fine. Apart from one thing. The parameter nodescription contains a non-descriptive random set of letters, numbers and other characters.
I would like to rewrite the nodescription parameter to a more descriptive one. I understand that I can do this with a rewritemap through Apache. However, I have no experience at doing soemthing like this, and I'm not entirely sure where to start.
Normally I would simply alter script.php so that it contains more descriptive parameters, but this time I have no control over the script; I am pulling it from another site using cURL.
Can anybody give me an example of how to pull this off?
Thanks!
Matt
Well, to answer my own question, to pull this off you need access httpd.conf file on your apache server. My shared hosting company didn't allow access to this file (I doubt any would allow you access).
So I bit the bullet and purchased a VPS. I will post the steps I took here in order to set the rewritemap up in the hope that it will help a lost soul :) Ok, here goes...
My VPS has WHM installed, so in WHM I went to:
Server Configuration >> Apache Configuration >> Include Editor
Pre Virtual Host Include >> All Versions
This feature takes any text you put in and includes it in your httpd.conf file without worrying that it will be overwritten at a later stage. If you don't have WHM on your server then you can add the text directly to your httpd.conf file; make sure it is outside and before any virtual hosts.
OK, so I included the following map declaration and rewrite rule:
#Map to redirect (swaps key and value)
RewriteMap rwmap txt:/home/*/public_html/rdmap.txt
<Directory /home/*/public_html/test>
Options All -Indexes
RewriteEngine on
RewriteRule ^url/([^/]*)/$ /script.php?foo=${rwmap:$1|$1}&rewrite=on [L]
</Directory>
The actual map is a simple text file containing key/value pairs - you need to place this file in the directory declared in RewriteMap rwmap txt:/home/*/public_html/rdmap.txt.
And there you go. Apache now rewrites my URLs for me and I now have some nice and pretty SEO optimized links thanks to my rewrite map! Hoorah!
RewriteEngine on
RewriteRule ^green/([^/]*)/(.*)$ /script.php?somevar=green&nodescription=$1&rewrite=on [L]
This rewrite will allow you to pass "arbitrary text" that has nothing to do with the rewrite. For example:
http://www.mysite.com/green/aA43-/some-seo-boosting-title
Will still reroute correctly to script.php; the latter part will simply be ignored by the rewrite.

How to configure apache (ubuntu) such that www.mysite.com will direct to www.mysite.com/drupal6/?

I am a newbie to ubuntu and apache. Can someone tell me how I could direct to
www.mysite.com/drupal6
when user address www.mysite.com?
Thanks a lot.
Cheers.
If you are running Apache and Ubuntu, there is actually a really easy way to force this redirect using a simple php script.
Create an index.php file in the root of your server and paste the following code into it
<?php header("location: drupal6/") ?>
This will cause the site to auto-redirect to the drupal6 folder whenever it is visited.
This should work. Create a file in the root folder of your server called .htaccess - the dot at the beginning is very important as this helps the server identify the file as a hidden / system config file.
Open the file and paste the following lines of code in :
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ www.mysite.com/drupal6/$1 [R,L]
This should force all traffic to the server to redirect to your custom folder.
A brief explanation of the .htaccess code
If you want rewrites to work, you have to enable the Rewrite Engine and tell the server to follow symlinks.
The second section establishes the rule - specifically applying it to all traffic on the standard web port of 80.
The final line tells the server to grab everything after the URL and append it to the new address (mysite.com/drupal6).
There's a lot more you can do with .htaccess files but you really need to Google for good examples to test out.
Look at Apache's mod_rewrite documentation. You will need a RewriteRule in your apache configuration at the minimum, you may also need RewriteCond's to define when the RewriteRule is used.
Your rewrite pattern will be rewriting the REQUEST_URI with something from: ^/$ to: /drupal6. The ^ and $ are essential to prevent Apache getting into an infinite loop while rewriting the base URI by only matching "/" and not "/anything-else".
I assume you're on a recent version of Ubuntu and Apache? If so, see the Apache 2.2 documentation on mod_rewrite.