11

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] ...


10

You shouldn't need to use percent encoding/hexcode in mod_rewrite parameters since mod_rewrite will encode special characters (?, #, , &, etc.) by default. To write a space in the rewrite pattern, use \s or just escape the space with a backslash (\). Whether space gets encoded as %20 or + depends on whether it's part of the query string or the URI. So ...


10

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 ...


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 ...


7

I will go onto something like that. First you define a new condition (RewriteCond) then you apply this condition using a rule (RewriteRule). Thanks to the flag L at the end of the RewriteRule, this stop the rewriting process immediately and don't apply any more rules. So when the request is a .css file, the first rule will be apply and not the second one. ...


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 ...


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

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

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

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

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 ...


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 ...


4

Finally realised what the problem was when, after deleting the tag in both files, the error "Invalid command '\xef\xbb\xbf EF BB BF = BOM Moral of the story: Always check your encoding* and hex dump of your files when you get weird issues. *On Notepad++ you can use the Encoding -> Encode in UTF-8 without BOM option to remove it


4

cPanel is not inherently involved here, by the way. Do you actually know if you're allowed to do this? Not all hosts allow such config changes via .htaccess. You should probably contact support to check. If you already know it's possible, you can add: RewriteEngine On to your .htaccess file. Just put it near the top so it's before any rewrite rules.


4

Try either RewriteRule ^first-page/$ /subdir/page.php?id_page=1 [QSA,L] RewriteRule ^second-page/$ /subdir/page.php?id_page=2 [QSA,L] RewriteRule ^third-page/$ /subdir/page.php?id_page=161 [QSA,L] or RewriteRule ^first-page /subdir/page.php?id_page=1 [QSA,L] RewriteRule ^second-page /subdir/page.php?id_page=2 [QSA,L] RewriteRule ^third-page /subdir/page....


4

If you are using Wordpress now, and simply trying to redirect old Joomla urls to your new urls I suggest using a plugin to make your life easier. I recommend this plugin a lot: http://wordpress.org/extend/plugins/redirection/ Also keep in mind that you should use fancy permalinks within Wordpress instead of the standard ID numbers. You can find this ...


4

Your regular expression has the following problems: It only has .* for the "site" and "sectionname", but is missing one for the "categoryname". Based on your example, there should be a third directory level in your regex. You have parenthesis around items which you don't need to reference, but no parenthesis around the ID, which you will need to reference. ...


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

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