14

You are looking for the Query String Append flag - e.g. RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^search/(.*)$ search.php?q=$1 [QSA] See the mod_rewrite documentation for a full description of RewriteRule flags.


12

Yes, if you have your URLs redirected properly. Yes. You should be using a 301 redirect instead of internally rewriting the URLs. No, this is completely unnecessary. If your old URLs are 301 redirected to the new URLs, then Google will know that those resources have been permanently renamed/moved. That's the whole point of having a 301 redirect code (as ...


10

Try this RewriteEngine On RewriteBase / RewriteRule ^edit/id/([0-9]+)/?$ edit.php?id=$1 [NC,QSA,L]


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

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


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


7

Thanks for your time to look at the question, but we appeared to have figured it out: Options -Multiviews -Indexes +FollowSymLinks RewriteEngine On RewriteBase / DirectorySlash Off # remove trailing slash RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L] # rewrite /dir/file?query to /dir/file.php?query RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-...


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


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

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]


5

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


5

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


5

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


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

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


5

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


5

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 blocking directives after the WordPress ...


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

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

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 simply rewrite to another domain, you would need to proxy the request. Using mod_rewrite in .htaccess... Verbose method: RewriteEngine On # domain1.com ...


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

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


4

Assuming that DirectoryIndex and AllowOverride All|Indexes are enabled in your Apache configuration, you can use the .htaccess to change the directory listing settings on a per-folder or location basis. Use the Location container to specify the location, then change the DirectoryIndex file to a file that doesn't exist. Apache will try to use it, the it will ...


4

If the user names (like kevinlee) are unique on the system, this is pretty straight forward. First, for simplicity, I would move the profile call to a profile.php page, and move it out of the /profile/ directory (I'm assuming it used to just be an index.php page in /profile/?). You don't have to do that, but it just makes the .htaccess work a little cleaner ...


4

The {REQUEST_URI} string starts with a / character. Apache changed regex engines when it changed versions, so Apache version 1 requires the leading slash while Apache 2 forbids it! We can satisfy both versions by making the leading slash optional with the expression ^/? (? is the metacharacter for zero or one of the preceding character). Source: ...


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