Hot answers tagged

4

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 the regular expression. Well, yes unfortunately... RewriteEngine on RewriteCond %{HTTP_HOST} !^(wiki|sub2|sub3)\. RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) ...


4

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]


4

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


3

tl;dr You need the L flag on your redirects, ie. [R=302,L]. RewriteRule ^(.*)$ http://meow.co.uk/$1 [R=302] Bit of an aside, but... the RewriteRule pattern should be ^bar/meow/(.*) (as mentioned in my answer on your other question), otherwise you won't get the working redirects that you've stated. For external redirects you generally want to use the L ...


3

You want to do a blanket redirect from one domain to another. Assuming Apache, put in your .htaccess file in the root directory of example0.com: RewriteEngine on RewriteRule ^(.*)$ http://www.example1.com/$1 [L,R=301] This will redirect all requests from 0 to 1 and keep the original request URI.


3

If you're getting alot of requests with undefined added to the URLs and you know the things accessing those URLs are people and not robots, you're much better off using HTTP status code 301 and redirecting the URL to the correct one. Using status code 204 will not help because it means "No content" and the user will then need to manually modify the URL in ...


3

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


3

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


3

Never mind. I found the answer I needed to my problem. It turned out I also had a folder named m with the file robots.txt in it. I then removed that entire folder and now the RewriteRule works.


3

...does that mean that all http to https "rewrites" are actually redirects? Yes. They are external redirects, not internal rewrites. But rewrite does all the changes server side and are invisible to the client. Yes, most would agree that a "rewrite" is internal to the server. And a "redirect" is external. Unfortunately, these two terms are often ...


3

RewriteRule ^lp/?$ /index.php [L] This fails to match because your actual URL is probably /lp/index.php (or whatever your DirectoryIndex is), not simply /lp/. (That is after Apache has internally mapped the URL to a filesystem path). So, try changing your RewriteRule pattern: RewriteRule ^lp/index\.php$ /index.php [L] Specifically, there is a ...


2

The problem here was ErrorDocument which doesn't interpolate server env vars...


2

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


2

I personnaly use this: RewriteEngine on RewriteCond %{HTTP_HOST} !\..+\. RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L] It checks if there is NOT 2 dots in the domain name. This is very similar to what w3d proposed, I just find it more readable. And as w3d stated, it is impossible to have a fully generic solution because nothing allow to guess how ...


2

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


2

RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] If you are redirecting from one completely different domain to another then you would not expect to be able to use %{HTTP_HOST} in the target. For example, the above would result in example.rhcloud.com being redirected to www.example.rhcloud.com - which is not ...


2

Method 1 With RewriteCond you can add this (after your existing rules): RewriteCond %{HTTP_HOST} ^www\.example [NC] RewriteCond %{HTTP_HOST} \.(jpg|jpeg|png|gif|ico|icon)$ [NC] RewriteRule .* https://example.com%{REQUEST_URI} [L,R=301] If the request is done on www.example.com, and the file requested end by jpg or jpeg or png or gif or ico or icon (case ...


2

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


2

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] RewriteCond %{REQUEST_URI} !.jpg$ [NC] RewriteCond %{QUERY_STRING} !^page RewriteRule ^(.*)/?$ /something.php?page=$1 [L] It scans the URL for each extension one by ...


2

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


2

Welcome to WSE. Nginx doesn't support .htaccess and if you trying to use .htaccess with Nginx you are doing it incorrectly. Apache's .htaccess is powerful and, as I have now learned (thanks @closetnoc), it caches the .htaccess file until it changes on the disk to avoid reloading it each and everytime. However, you wanted Nginx for it's speed didn't you, so ...


2

Facebook would have been using the image URL as specified in the open graph tags. Since (by the sounds of it) the URL structure wasn't actually changed in WP (only in .htaccess) then this would have contained the /blog subdirectory in the URL. But then presumably you had to make an exception in .htaccess for the /blog/wp-content/uploads/ directory, or was FB ...


2

as @w3dk mention in the comments, I had to add a R=301 in my .htaccess file. RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R=301,L] </IfModule> I didn't try Goyllo's answer but I think it's a good one if you don't have access to the server.


2

If the query string is irrelevant then it can simply be ignored. (You can't match the query string anyway with the RewriteRule directive.) Try the following in your root .htaccess file to redirect the request: RewriteEngine On RewriteRule ^index.php/path/some-path/some-subpath$ /category/? [L,R=301] In per-directory .htaccess files, the URL-path matched ...


2

Try the following in the .htaccess file in the root of your site. RewriteEngine On RewriteCond %{HTTP_REFERER} ^http://forums\.referer-site\.com/thread1/ [NC] RewriteRule ^page\.html - [F] This will send a 403 Forbidden when someone follows a link to your /page.html URL from the referring site. There is always the risk that browsers won't send an HTTP ...


2

In order to match against the query string (everything after the first ?) you need to use a RewriteCond directive and check against the QUERY_STRING server variable. The RewriteRule pattern only matches against the URL-path. So, try something like the following in .htaccess, before any WordPress rewrites: RewriteCond %{QUERY_STRING} (.*) RewriteRule ^...


2

First off, RewriteCond %{REQUEST_FILENAME} !index.html won't work, you'll need !"index\.html", with the quotes. Otherwise it's not a valid regex. According to the docs, you should use the [END] flag instead of [L], to prevent further request processing (internally, your rewrite is reevaluated by the Apache redirect rules). Apparently, there's a bug with [...


2

To combine those three rules into one you can use alternation (with a vertical bar) in a capturing group and another backreference in the substitution: For example: RewriteRule ^(information|reviews|comments)/business-name/(.*)$ /$1/new-business-name/$2 [R=301,L] $1 is now a backreference to either "information", "reviews" or "comments". Depending on ...


2

All these appear to redirect OK for me (ie. http://bbrown.kennesaw.edu/it3203/2016_02/lab_7s.html redirects to http://ksuweb.kennesaw.edu/faculty/rbrow211/it3203/2016_02/lab_7s.html). So, this would seem to suggest a local caching issue. If you've previously experimented with erroneous 301 redirects, then these will have been cached by the browser and ...


2

RewriteCond %{REQUEST_URI} \/modeles\.php [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule brand=([A-Z][\-A-Za-z]+)&model=([\-A-Za-z0-9\.]+$) http://www.soeezauto.com/modeles-voiture/$1/$2 [R=301,L] You can't match the query string with the RewriteRule pattern. The RewriteRule only matches against the URL-path (...



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