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

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

You need to know the relationship between the project-folder and the sub-folder, ie. which sub-folder a particular project is in. If there is no pattern between project-folder and sub-folder and being restricted to .htaccess then I think you'll need to specify each rewrite manually. You can internally rewrite all requests for the respective project to ...


3

You can do this by adding another condition. RewriteCond %{REQUEST_URI} !^/members [NC] RewriteCond %{REQUEST_URI} !^/activity [NC] RewriteRule ^(.*)/([0-9]+)/$ /$1/S$2/ [R=301,L] Or, sometimes simpeler, use the OR of a regex: RewriteCond %{REQUEST_URI} !^/(members|activity) [NC] RewriteRule ^(.*)/([0-9]+)/$ /$1/S$2/ [R=301,L] I've removed the .*$ ...


3

You can just add another RewriteCond directive: RewriteCond %{REQUEST_URI} !^/activity RewriteCond %{REQUEST_URI} !^/members RewriteRule ^(.*)/([0-9]+)/$ /$1/S$2/ [R=301,L] Multiple RewriteCond directives are AND'd together by default. The OR flag can be used if required. I've removed the NC flag - unless you specifically need a case-insensitive match. As ...


2

It looks like that code was developed to stop bots probing humans.txt via query strings. Not Blocked: http://example.com/humans.txt Blocked: http://example.com/?some_path=http://www.google.com/humans.txt? There is plenty of online guides about blocking humans.txt additionally lots of websites explaining what does what, a lot of those rules in that block ...


2

RewriteCond %{REQUEST_URI} !^/help/ RewriteRule (.*) /4-3/help [L,NE,R] You are getting a redirect loop here because you are saying that if the URL is not /help/ then redirect to /4-3/help (etc, etc, ...). You need to reverse the logic and only redirect when it is /help/. Change to... RewriteRule ^/?help$ /4-3/help [R,L] NB: This is a temporary (302) ...


2

I may be missing something, but my first reaction is to not redirect (or rather internally rewrite?) if the file doesn't exist, but only when the URL does not already contain the version folder (eg. v01). For example, for .htaccess in the document root: # Internally rewrite to the latest version if not specified RewriteCond %{REQUEST_URI} !^/app/v\d\d/ ...


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


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

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


2

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.


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


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 ^(.*)$ ...


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


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

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.


1

I would aproach this another way since .htaccess is gonna be messy. Part of creating code is keeping it maintainable. No-one is ever gonna touch that code again. I'd use the help of another language like PHP (or another language you like) for this: # Redirect if the url isn't what I'd like RewriteCond %{REQUEST_URI} \s RewriteRule ^(.*) ...


1

I found the solution. RewriteCond %{HTTPS} off RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP_HOST} !^www\. RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


1

As I understand, you want requests for the following URL website.com/games/271/game-name-here to be handled by games.php For this, you may use a simpler rule. RewriteRule ^games/(.*)$ /games.php?var=$1 [L] This will send all requests with /games/ to games.php. For e.g. /games/271/game-name-here will go like this /games.php?var=271/game-name-here Now, ...


1

If the /account subfolder is intended to be outside of WordPress then you can do something like closetnoc suggests, however, you also need to enable the rewrite engine to prevent the WordPress rewrites in the parent .htaccess from taking over. So, in /account/.htaccess: ErrorDocument 404 /error.php # Enable the rewrite engine in the subdirectory to ...


1

In your .htaccess file: RewriteEngine On # Change to use subdomain RewriteCond %{HTTP_HOST} !^sos\.hello\.com$ [NC] RewriteRule ^(.*)$ http://sos.hello.com/article/$2 [R=301,L] # Change to pretty URL path if no query string parameters present RewriteCond %{QUERY_STRING} !id [OR] RewriteCond %{QUERY_STRING} !title RewriteRule ^(.*)$ ...


1

I'm sure there must be a more elegant/efficient way of writing this, but this should work to do what you require: RewriteEngine On RewriteBase / # Restrict users from IP address 10.8.0.11 to user1 sub-folder RewriteCond %{REMOTE_ADDR} 10\.8\.0\.11 RewriteCond %{REQUEST_URI} !(user1) RewriteRule ^(.*)$ /user1/$1 [L,R=301] # Restrict users from IP address ...



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