13 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 ...
user avatar
  • 41.2k
12 votes
Accepted

htaccess redirect non-www to www with SSL/HTTPS

I found the solution. Without HSTS (single redirect): RewriteCond %{HTTP_HOST} !^www\. RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTPS} off RewriteRule .* https://...
user avatar
  • 331
11 votes
Accepted

.htaccess rewrite URL leads to missing CSS

Not being able to find your JavaScript and CSS files is a client-side/browser issue related to your URL-path, it's not something that should be fixed in .htaccess (at least not in this case) - ...
user avatar
  • 41.2k
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)...
user avatar
  • 41.2k
9 votes
Accepted

Apache Rewrite Rule non-www to www + http to https + remove trailing slash

For your first two rewrites (non-www → www, http → https), the following rule should work: RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !=www.example.com [NC] ...
user avatar
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: ...
user avatar
  • 181
8 votes
Accepted

Rewrite up to three folders into three query string parameters to a PHP script

You can use these three rewrite rules which handle up to 3 levels of directories: RewriteEngine on RewriteRule ^main\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? /parser.php?var1=$1&var2=$2&var3=$3 [L] ...
user avatar
8 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 ...
user avatar
  • 41.2k
7 votes

Remove query string from 301 redirect URL

By default the query string on the requested URL is appended to the rewritten/redirected URL. On Apache 2.4+ you should use the QSD (Query String Discard) flag on the RewriteRule directive in order ...
user avatar
  • 41.2k
7 votes
Accepted

How to do 301 redirect for complete URL?

redirect 301 http://m.somesite.com/site/somesite/faqs http://www.somesite.com/faqs/ This doesn't work because the source URL needs to be a URL-path, starting with a slash (as you have used for the ...
user avatar
  • 41.2k
7 votes
Accepted

Apache .htaccess mod_rewrite: use of [NC]

But the Official documentation clearly states it is used only for the RewriteRule directive. The docs don't say that it can only be used on the RewriteRule directive. The page you link to (which ...
user avatar
  • 41.2k
6 votes

htaccess redirect non-www to www with SSL/HTTPS

Your conditions are implicitly AND'd and your second condition will always be true (unless you have other domains), so your current rules will only redirect non-SSL traffic. You need to OR the ...
user avatar
  • 41.2k
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: ...
user avatar
  • 41.2k
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,...
user avatar
  • 6,736
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 $ ...
user avatar
  • 41.2k
6 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 ...
user avatar
  • 41.2k
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 ...
user avatar
  • 41.2k
5 votes
Accepted

redirect 204 with htaccess if url contain certain string

In order to match "undefined" at the end of the URL-path you need the regex pattern undefined$. The pattern ^undefined$ (which you've used in your question) matches the exact URL "undefined", which is ...
user avatar
  • 41.2k
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 ...
user avatar
  • 41.2k
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 ...
user avatar
  • 32.6k
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 ...
user avatar
  • 41.2k
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, ...
user avatar
  • 41.2k
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 ...
user avatar
  • 41.2k
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 ...
user avatar
  • 41.2k
5 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$ ...
user avatar
  • 41.2k
4 votes

How to write a generic .htaccess file rule, regarding RewriteBase

Trying the same and on my search I found that symfony2 frameworks .htaccess file is doing that at least it is stated in the file that it would. I tried to use it in my own setup but didn't work. Maybe ...
user avatar
4 votes

How can I get a regular expression to only match an empty string? (In a .htaccess file)

I wasn't convinced this would work, but I've tested it and it does seem to. Quite simply: ^.{0}$ Where: ^ is the start of the match . is any character (except newline) {0} is precisely zero ...
user avatar
4 votes

Redirect non-www to www : .htaccess without giving the domain name

I don't think you can make it completely generic, since you'll need to make exceptions for your subdomains, unless there is a pattern to your subdomains? A workaround is to add each subdomains to ...
user avatar
  • 41.2k
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] ...
user avatar
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 ...
user avatar

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