13

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 directives in the wrong order is a common cause of error. With WordPress, people often make the mistake of including canonical redirects (ie. HTTP to HTTPS and ...


12

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://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] With HSTS (double redirect): RewriteCond %{HTTPS} off RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] ...


11

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) - although it is because you are changing this URL-path (in .htaccess) that you are experiencing this problem. This problem is caused by using relative URLs in your HTML....


11

...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) .htaccess files are not necessary if you have access to the Apache server config. In fact, it is preferable to use the server config instead of .htaccess ...


9

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] RewriteRule ^(.*) https://www.example.com/$1 [NS,L,R=permanent] Just replace www.example.com with the actual canonical hostname of your site. As for your third ...


8

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: example.com/fruit/apple) if it appears in a .htaccess file: RewriteRule ^(fruit|fruits)/apple http://newfruitwebsite.com$1 [R=301,L] but will not match in ...


8

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 them in a <IfModule mod_rewrite.c> container. Because if they are required and mod_rewrite is not available then the directives simply fail silently and ...


7

You're almost there. Drop the very first RewriteCond (as it's not required and won't match ever) and add a trailing / to your last RewriteRule since you're redirecting with them. RewriteEngine On RewriteCond %{HTTP:Accept-Language} ^en [NC] RewriteRule ^$ http://mysite.com/en/ [L,R=301] RewriteCond %{HTTP:Accept-Language} ^de [NC] RewriteRule ^$ http://...


7

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] RewriteRule ^main\/([^\/]+)\/([^\/]+)\/? /parser.php?var1=$1&var2=$2 [L] RewriteRule ^main\/([^\/]+)\/? /parser.php?var1=$1 [L] In those regular ...


7

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 incidentally is specifically about the RewriteRule flags) simply states: Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. ...


6

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 to discard the query string from the redirected URL. For example: RewriteCond %{QUERY_STRING} =p=15 RewriteRule ^seasonal/christmas$ /holiday-decor/christmas....


6

The way that you will do the 301 redirect does not play any role for the Google. Actually it does not see your configuration file. What it is important is to send the 301 headers with the new location on each old url and not just the home page. For example if you have 10 pages on the old website, make sure that these all pages are redirected to the new 10 ...


6

Just redirect by 301 HTTP status all your old URLs to the new ones. If you only change file extension, you can do it easily with an .htaccess file (if you use Apache as a web server). Put these lines in your .htaccess file: RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php Of course, change www.example.com by your domain name. By using 301 ...


6

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 redirects that work), not an absolute URL. In other words, it should be written as the following (in a .htaccess file located at the subdomains document root): ...


6

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 conditions and negate the www (second) condition: RewriteEngine On RewriteCond %{SERVER_PORT} !=443 [OR] RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ https://...


6

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: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{QUERY_STRING} !^page RewriteRule ^(.*?)/?$ index.php?...


6

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,L]


6

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 have a customised Apache server then mod_rewrite always executes before mod_alias, despite the apparent order of the directives in the Apache config file (eg. ....


5

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 never going to match, unless the request is for http://example.com/undefined. This directive should go at the top of your .htaccess file (after the ...


5

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 $ anchors then you are only going to be matching part of the URL. eg. anything$ is going to match "anything" at the end of the URL - this could match too many ...


5

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 special to "ignore existing directories and rewrite everything to public dir". So again, there must be something else going on here? However, what you do need to ...


5

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 hesitate to plonk your code as an answer. SOURCE RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^domain\.com$ [OR] RewriteCond %{HTTPS} off ...


5

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 first rule, but would conflict with the second) and then prefixes this to the requested host. I assume you also want to redirect http://example.com/<...


5

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, Apache uses the PCRE flavour of regex.) Would that just be RewriteCond %{QUERY_STRING} ^m=*$ No. This assumes the argument is a simpler wildcard type ...


5

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 processed as a regex. However, in real terms there is no measurable performance difference between these particular expressions. It's a matter a personal ...


5

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$ /$1 [R=301,L] The regex (.*)index\.html$ matches any URL-path that ends with index.html and captures the part of the URL-path before index.html. The $1 ...


4

This should work for files of any extension: RewriteEngine on RewriteCond %{QUERY_STRING} ^/(.*\..*)$ RewriteRule ^index\.php$ http://www.newsite.com/%1? [L,R=301] Or to redirect the entire path and file request: RewriteEngine on RewriteCond %{QUERY_STRING} ^/(.*)(\..*)$ RewriteRule ^index\.php$ http://www.newsite.com/%1%2? [L,R=301]


4

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 that will help you guys somehow. # Determine the RewriteBase automatically and set it as environment variable. # If you are using Apache aliases to do mass ...


4

The pseudo-code translation of your .htaccess file would be something along these lines: Line 1: In case we weren't previously planning to do anything special with URLs, we are now (RewriteEngine is an optional processing module and we're making sure its enabled). Line 2: When we're talking about rewrite URLs, from here on append the path / to the beginning ...


4

You can use one of these solutions: Keep the Current .html extension unchanged for just frontend purpose and use Apache .htaccess file to proxify your requests so that. Add this to your .htaccess file to work: RewriteEngine on RewriteRule ^(.*).html $1.php OR Why not just have the HTML pages parsed as PHP? That way you can get the best of both php ...


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