Questions tagged [.htaccess]

Directory-level configuration file found mostly on Apache web servers

The htaccess file is a supplemental configuration file used by Apache to modify the default configuration as it applies to the directory the htaccess file it in (as well as all the files and subdirectories in that directory).

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.
Source: Apache manual

The directives allowed in htaccess files are governed from the main Apache configuration files by means of the AllowOverride directive.

The most common use is in conjunction with the mod_rewrite and security modules of the Apache webserver. Mod_rewrite allows URLs to be rewritten for scenarios such as the following:

  • Shortcuts such as example.com/info to redirect to another page
  • Redirects from old pages to new pages
  • virtual directories (directories which actually appear elsewhere in the file system)

The most common security applications are:

  • Password protection of directories and files

For more information see the mod_rewrite and aaa documentation.

2807 questions
1
vote
1 answer

Apache 2.4 .htaccess Set Headers for error pages

I am setting some headers using .htaccess for a web page, f. e.: Header set Strict-Transport-Security "max-age=31536000; includeSubDomains" This works fine for usual page access, but the headers are not used on…
MW.
  • 113
  • 4
1
vote
1 answer

Add a subdomain as an exception in hotlinking

I used this code in my .htaccess file and it is working great to prevent hotlinking: RewriteEngine On RewriteCond %{HTTP_HOST}@@%{HTTP_REFERER} !^([^@]*)@@https?://\1/.* RewriteRule \.(gif|jpg|jpeg|png|tif|pdf|wav|wmv|wma|avi|mov|mp4|m4v|mp3|zip?)$…
jeff
  • 11
  • 1
1
vote
1 answer

RewriteRule won't match if trailing slash present, throws nginx 404

With this .htaccess... RewriteEngine on RewriteRule ^foo$ foo.php RewriteRule ^foo/$ foo.php ...I get the following behavior, when I request /foo — works, I get foo.php /foo/ — fails unexpectedly with a bare 404 from nginx /foobar — fails, as…
kubi
  • 121
  • 6
1
vote
1 answer

rewrite rules from apache to nginx

I have a problem with this... I'm using free hosting for giving a demo to my future buyers of my own cms. The hosting is hubuhost.com, they are supporting the last version of php 8.1. The problem is that they are on nginx and do not support my…
pok4
  • 13
  • 4
1
vote
2 answers

.htaccess mod_rewrite not catching all RewriteRules

There is a PHP application with a PHP router as entry point for all the requests placed inside index.php. I am trying to write a .htaccess file to forward every request to index.php except for API requests and for design resources. So I am trying to…
caffeine
  • 113
  • 5
1
vote
2 answers

redirect get URL?

I'm almost done writing the .htaccess file to redirect some URLs to a new domain. One final thing: I have URLs with this structure: http://www.domain.be/?s=searchterm How do I capture them? RewriteRule ^\?s=(.*)$ …
Wolfr
  • 23
  • 3
1
vote
2 answers

Htaccess to rewrite URL with two parameters

I want to rewrite this type of URL https://mywebsite.com/content/page.html?id=12&title=my-page-title to https://mywebsite.com/12/my-page-title.html Using generateit.net/mod-rewrite/ I got the following rule: RewriteEngine On RewriteRule…
cat15ets
  • 21
  • 1
  • 3
1
vote
1 answer

How to make mod_rewrite force a redirect?

I have a need to temporarily redirect my website to a static version hosted on Cloudflare. The current site is a Wordpress site hosted at Ionos and Wordpress seems to be doing a 301 redirect from mydomain.com to www.mydomain.com. I would like to be…
Darrell Brogdon
  • 137
  • 1
  • 1
  • 11
1
vote
1 answer

Rewriting to point to a CDN

I'm hoping to use a CDN to deliver all our image files. We call the images by simply using the CDN's domain instead of our own. I could easily change the domain reference in our content management system, but then preview breaks because it's looking…
Al Qbeck
  • 13
  • 3
1
vote
2 answers

How to use Mod Rewrite to access non-document-root folder files?

I have the following structure for my website codes at /var/www/html -- files ---- test.txt -- app ---- SomePhpCodes.php ---- SomePhpCodes.php -- public ---- index.php ---- .htaccess The document root is set to be "/var/www/html/public" but I…
cr001
  • 195
  • 2
  • 8
1
vote
1 answer

Apache .htaccess causing images to load extremely slowly

I am running a single page application that requires the following lines on my .htaccess . However, images on its /images/ folder are now loading extremely slowly. Is there anything wrong with the following lines? RewriteEngine On RewriteCond…
user59067
  • 205
  • 1
  • 3
  • 7
1
vote
1 answer

htaccess rewrite rule not working with file extentions

I have two domains setup on the same folder on a server. i.e. dl.d123.com @ /var/www/public_html/dl.domain123.com dl.domain123.com @ /var/www/public_html/dl.domain123.com I have an htaccess file in /var/www/public_html/domain123.com with the…
iraqiboy90
  • 21
  • 4
1
vote
1 answer

Help with mod_rewite rule

I have a rule that will get the extension of every file from the url. I need to match all jpg, gif, png and bmp files. This is for a watermark application. Currently, it only matches jpg and Jpg. Can someone help me match all four extensions? Here…
timmay
1
vote
3 answers

Deny Access to directories from unauthorized users

I am not being paid for this and I would like to know the quickest way to do the following. A former client has a page which only members can access. This page links to a number of galleries which he only wants members to access. The galleries are…
ComputerUser
  • 111
  • 4
1
vote
1 answer

Apache .htaccess require ip not strict enough with IPv6

Let's suppose my IPv6 is 1111:2222:3333:4444:5555:6666:7777:8888 I can write any of these lines in the Apache .htaccess: Require ip 1111:2222:3333:4444:0000:0000:0000:0000 Require ip 1111:2222:3333:0000::/64 Require ip…
Daxorp
  • 11
  • 1