Disable HTTP trace in .htaccess - regex

Is it possible to turn off HTTP trace method via .htaccess using this directive?
TraceEnable Off
When I try to add this directive in .htaccess I get an internal server error, maybe it's only allowed in my main httpd.conf file?

Unfortunately this directive isn't allowed in .htaccess as per official docs:
TraceEnable Off
You need to put this in Apache config or your vhost setup.
In .htaccess you can use a workaround (as per the link shared by #donald123):
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule ^ - [F]

take a look on this http://www.ducea.com/2007/10/22/apache-tips-disable-the-http-trace-method/ it should be a solution for you

Related

I want to redirect example.com/chapter-foo.php to example.com/chapter.php

I have a site with some pages like that:
example.com/dogs-foo1.php
example.com/dogs-foo2.php
example.com/dogs-foo3.php
And then
example.com/cats-foo1.php
example.com/cats-foo2.php
example.com/cats-foo3.php
Now I have simplified the site with tab menus and I only have
example.com/dogs.php
example.com/cats.php
Now I want the people who try to go to: example.com/cats-foo1.php
be redirected to: example.com/cats.php
instead of getting a 404
Is there anyway, maybe with htaccess?
This probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?cats-.+.php$ /cats.php [R=301]
For this to work you will need to enable the rewriting module into your http server. You should place it in the http servers host configuration. If you decide to use a dynamic configuration files instead (.htaccess) you need to enable their interpretation first (see the AllowOverride directive in the official documentation).
I would recommend however to go a step further and use URLs along the pattern https://example.com/cats, so without the trailing .php as is the standard these days. You need some additional internal rewrite rule for that:
RewriteEngine on
# external redirect from /cats-foo.php to /cats
RewriteRule ^/?cats-.+\.php$ /cats [R=301]
RewriteRule ^/?cats\.php$ /cats [R=301]
# internal rewrite
RewriteRule ^/?cat$ /cats.php [END]
This again can be generalized:
RewriteEngine on
# external redirect from /cats-foo.php to /cats
RewriteRule ^/?(\w+])-.+.php$ /$1 [R=301]
# internal rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(\w+])$ /$1.php [END]
If you experience a http status 500 with that rule in place chances are you operate a very old version of the apache http server. You will find entries in your http servers error log files complaining about the END flag. In that case replace it with the L flag and try again.
You probably will have to adjust those line, we don't know your specific situation. But the above should get you started along with reading the documentation of the tools you use, which you can start here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

URL Mask/Cloak with redirect

Am trying to do the following. My website is hosted on
www.sitehost.com/uk
But I own this domain.
www.mainsite.co.uk
Is it possible to redirect the user hitting www.mainsite.co.uk to www.sitehost.com/uk but retain the www.mainsite.co.uk?
I tried doing .htaccess redirect and it worked but it changed the URLs from www.mainsite.co.uk to www.sitehost.com/uk
Ideally it would work like so...
www.sitehost.com/uk/post/20
can be accessed via
www.mainsite.co.uk/post/20
I tried mod_proxy but it didn't seem to work all the way. Anyone know how to do this? Is this even possible with Apache?
This is possible if mod_proxy is enabled in your Apache config.
Once mod_proxy and mod_rewrite are enabled place this rule in your DocumentRoot/.htaccess file of sitehost host:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?sitehost\.com$ [NC]
RewriteRule ^ http://www.mainsite.co.uk%{REQUEST_URI} [L,P]
P flag is used for proxying the request to external URL.

Redirecting home to page in subdirectory, mask url

I'm using wordpress.
I'd like my site's domain root to display contents of another page while still showing my domain root as the url.
EXAMPLE: "http://www.mysite.com" displays contents of "http://www.mysite.com/blogs/home"
but users still see "http://www.mysite.com" in the address bar.
I've managed to get the redirect working, but it's not masking the URL. It essentially is redirecting.
Here's what I have now in my htaccess:
RewriteEngine on
RewriteRule ^\/?$ \/blogs\/home\/ [R,NC]
The R option forces an external redirect. Try just [NC] instead
what about this way.
put the index.php and .htaccess file in the root of your domain.
edit index.php to grab the "wp-blog-header.php" file from what ever location you put it. For example, if your wordpress file is located in blogs/home/ sub directory, you change the index.php file located in the root directory as require('./blogs/home/wp-blog-header.php').
forget about the redirect issue.
I think this is the easiest way. Or did I misunderstand your question?
There is a very simple solutions to this problem. In fact wordpress has an option to accomplish this task.
Go to Dashboard->Settings->Reading->Front page displays
Select a static page as front page.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^$ /blogs/home [NC,P]
PS: Make sure you have mod_proxy enabled in your Apache config.

Can't get a simple RewriteRule in .htaccess to work in local WAMP

I've enabled mod_rewrite, restarted all services, still not taking affect. Here's what I have in .htaccess:
RewriteEngine on
RewriteRule .* localhost/phpinfo.php
Please advise.
First of all rewrite rules in general do not affect the host part of the URL (unless you specify predefined names like REQUEST_URI etc.) just the path part (an excellent article explaining the bits and bytes of URL rewriting.) That said your rules should at least look something like this.
RewriteEngine On
RewriteRule (.+) /phpinfo.php [L]
Furthermore is your AllowOverride directive for that particular vhost (or folder) configured to allow .htaccess files at all? If not .htaccess files will be completely ignored. In order for mod_rewrite to work it needs to be set at least to FileInfo Options.
AllowOverride FileInfo Options

How do i convert this Mod_rewrite rule to nginx

This is the Htacces rule:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ ir.php?id=$1
how should I pass it to a Nginx compliant rewrite rule... i read the doc and did this:
rewrite ^([A-Za-z0-9-]+)/?$ ir.php?id=$1 last;
But didnt work.
and another question:
Is there any equivalent of .htaccess to Nginx (per directory rules)
Thanks
As an answer to your second question: no, there is no equivalent for Apache's .htaccess files for per directory configuration. You can have per directory configuration files included via the master config, but Nginx would need to be reloaded every time a config include file changed.
Finally, could solve it... rewrite ^([A-Za-z0-9-]+)/?$ ir.php?id=$1 last; To rewrite ^/([A-Za-z0-9-]+)/?$ /ir.php?id=$1 last;