16 votes
Accepted

.htaccess syntax multiple RewriteEngine on

Does the order of the code snippets play a role? Is it correct in the above example? Yes, the order of the directives in Apache config (.htaccess) files can be important. In fact, simply having ...
MrWhite's user avatar
  • 42.7k
11 votes
Accepted

Is there an alternative to .htaccess?

...in case the webhoster disabled the use of .htaccess files? If the webhost has disabled the use of .htaccess files then there is no direct alternative. (.htaccess = per-directory Apache config file)...
MrWhite's user avatar
  • 42.7k
9 votes
Accepted

Is Checking For mod_write Really Necessary?

No, most of the time, checking for mod_rewrite is not necessary. In fact, it is often preferable to remove this check. If the mod_rewrite directives are required by your site then you should not wrap ...
MrWhite's user avatar
  • 42.7k
8 votes

When is the leading slash (/) needed in mod_rewrite patterns?

I recently happened across this issue myself. There is a difference between using RewriteRule in the VirtualHost and as part of an .htaccess file. The following rule will match (with a URL like this: ...
Henders's user avatar
  • 181
8 votes
Accepted

RewriteRule started returning 403 Forbidden error

For some reason, without me making any changes, those pages have started returning a 403 Forbidden error. It's likely the version of Apache has been updated on your server. I answered a similar ...
MrWhite's user avatar
  • 42.7k
7 votes
Accepted

Execution order of different htaccess redirects

Redirect belongs to mod_alias, whereas RewriteRule is a mod_rewrite directive. Two different modules. Different modules execute independently and at different times during the request. Unless you ...
MrWhite's user avatar
  • 42.7k
7 votes
Accepted

Redirect from index.html to its directory using .htaccess?

You can do this using mod_rewrite near the top of your .htaccess file. In its simplest form this would be something like: RewriteEngine On # Remove "index.html" RewriteRule (.*)index\.html$ ...
MrWhite's user avatar
  • 42.7k
7 votes

AH10411: Rewritten query string contains control characters or spaces

As pointed out by Rohit Gupta in a comment, there was a change to Apache2 outlined in this git pull request, I just had to change my RewriteRules to include the B and BNP flags. For example: ...
Alexis Wilke's user avatar
  • 2,235
6 votes
Accepted

How to redirect all that URLs that does not end with "feed" to https?

You can use $ which means "Ends" RewriteEngine on RewriteCond %{HTTPS} !=on RewriteCond %{REQUEST_URI} !/feed$ # "Ends with /feed" ------------^ RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,...
Martijn's user avatar
  • 6,846
6 votes
Accepted

mod_rewrite ignore Stylesheet and JavaScript

Instead of making exceptions for a growing list of expected file types, it is more usual (and more flexible) to just make an exception if the file exists (or, optionally, is a directory). For example: ...
MrWhite's user avatar
  • 42.7k
6 votes
Accepted

Can rewriterules work without carets or dollar signs?

You are using the ^ and $ (anchors in regex speak) because you are matching the whole URL, which is what most people want to do, so this is the most common example you see. If you omit the ^ and/or $ ...
MrWhite's user avatar
  • 42.7k
6 votes

HSTS implementation in .htaccess when using www subdomain

using htaccess ( because its a shared environment. Using Cpanel. Aside: I would be wary about "preload list submission" on a shared server, since you don't have complete control (and it is ...
MrWhite's user avatar
  • 42.7k
6 votes
Accepted

What does ^(.*) mean in a rewrite rule, is there a simpler alternative?

As @dan mentioned in comments, this is a regular expression ("regex" for short and often referred to as a "pattern" in Apache docs). Specifically, Apache uses the PCRE (Perl ...
MrWhite's user avatar
  • 42.7k
6 votes
Accepted

Redirect requests on alternate port for Apache virtual host

<VirtualHost *:80 *:2030> Separate the vHosts, so that port 2030 has its own container and implement a site-wide redirect in that container to port 80 (or HTTPS/443?). For example: <...
MrWhite's user avatar
  • 42.7k
5 votes

.htaccess Direct all requests to Public dir when using framework etc

This code works fine You must have something else... the code you have posted would result in a rewrite loop (500 Internal Server Error) for all requests? But neither do you have to do anything ...
MrWhite's user avatar
  • 42.7k
5 votes

WordPress HTTP to HTTPS without trailing slash results in double redirect

I have managed to get this working by using a snippet provided by anubhava on Stack Overflow. If anyone spots a potential issue with this code or has a better and cleaner redirect then please don't ...
Simon Hayter's user avatar
5 votes
Accepted

.htaccess to WWW And/Or HTTPS

all of them have redirected http://sub.example.com To https://www.sub.example.com That would seem to suggest that you have a rule that simply checks for the absence of www (which is correct for the ...
MrWhite's user avatar
  • 42.7k
5 votes
Accepted

Simple .htaccess language question (redirection)

This is as much a question about regular expressions (regex) than .htaccess. The second argument (CondPattern) to the RewriteCond directive takes a regex, not a wildcard expression. (Specifically, ...
MrWhite's user avatar
  • 42.7k
5 votes

HSTS Preload section on .htaccess

...advise RewriteCond %{HTTPS} off and others have RewriteCond %{HTTPS} on That wouldn't make sense in the context given as these are obviously opposites (I would be interested to see the full ...
MrWhite's user avatar
  • 42.7k
5 votes
Accepted

500 internal server error when trying to implement reverse proxy under Apache. How do I forward the requests to another server without errors?

Thanks @MrWhite for giving the correct configuration. I needed extra module mod_proxy_http and to change the IP adress of server-B to the hostname as I have multiple vHosts on :80. Here is the ...
wouter205's user avatar
  • 337
5 votes
Accepted

In .htaccess files, is there any difference at all between “!=on” and “off”

tl;dr In this example, the net result is the same, but the pattern !=on is arguably more efficient because it is a lexicographical string comparison, not a regular expression. Unlike off, which is ...
MrWhite's user avatar
  • 42.7k
4 votes

mod_rewrite ignore Stylesheet and JavaScript

This should work: RewriteEngine On RewriteCond %{REQUEST_URI} !\.png$ [NC] RewriteCond %{REQUEST_URI} !\.html$ [NC] RewriteCond %{REQUEST_URI} !\.php$ [NC] RewriteCond %{REQUEST_URI} !\.css$ [NC] ...
Mike -- No longer here's user avatar
4 votes
Accepted

Redirect percent encoded URL in .htaccess

You can use mod_alias RedirectMatch, for example: RedirectMatch ^/سیستم-rss-سایت-بهینه-شد/$ http://www.example.com/ Note that the URL-path matched by RedirectMatch is %-decoded. However, if you are ...
MrWhite's user avatar
  • 42.7k
4 votes
Accepted

Have Apache show 404 at missing page URL instead of redirecting to error page URL

Apache server can be configured to show the error page at the error URL, or it can redirect to the error page. It is almost better to show the error page directly at the URL rather than redirecting ...
Stephen Ostermiller's user avatar
4 votes
Accepted

.htaccess several domains to point to different locations

Since all the domains point to the same filesystem, you don't need to think about the main_domain.com. Although if you did need this (for some back-end reason?) it complicates matters, as you can't ...
MrWhite's user avatar
  • 42.7k
4 votes
Accepted

rewrite url - remove trailing / if any and add .php

I assume you want to remove the trailing slash (if any) with an external redirect in order to canonicalise the URL and then append the .php extension with an internal rewrite (ie. hidden from the user)...
MrWhite's user avatar
  • 42.7k
4 votes
Accepted

Send "410 Gone" response for URLs that are a "c" or "g" followed by digits

RewriteCond %{QUERY_STRING} c Not sure why you are referencing the QUERY_STRING here? Your URLs don't appear to contain a query string? It looks like the URLs you want to block start with a c or g ...
MrWhite's user avatar
  • 42.7k
4 votes
Accepted

Redirect all index.html pages to subdirectory?

First, a couple of assumptions: All your internal links must already point to the non-index.html URL, eg. example.com/service/. (However, since you state that "every page is ending in the ...
MrWhite's user avatar
  • 42.7k
4 votes
Accepted

Redirect from URL with parameters to SEO friendly URL in .htaccess

tl;dr # Redirect /Category.php?Category_Id=10&Category_Title=some-text RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{QUERY_STRING} ^Category_Id=(\d+)&Category_Title=([\w-]+)$ RewriteRule ...
MrWhite's user avatar
  • 42.7k
4 votes
Accepted

Wrong URL Requested But I know what they meant - 404 vs canonical vs 301 redirect

tl;dr You should 301 redirect to the canonical URL. This is for the benefit of SEO, as much as user experience. You already know the correct resource to return (based on the company-25 part of the ...
MrWhite's user avatar
  • 42.7k

Only top scored, non community-wiki answers of a minimum length are eligible