Forum Moderators: phranque
[edited by: not2easy at 1:10 am (utc) on Dec 23, 2019]
[edit reason] readability [/edit]
What's the best way to do this with .htaccess?Stock response: What have you tried so far?
I don't want to serve a 404 (since the requested files and directories don't even exist),
and I don't want them around anyway,
I want to redirect them elsewhere via htaccess. What's the most elegant way to do that?
RewriteCond (REQUEST URI anything with wp-admin or trigger words in it)
RewriteRule (redirect to http://example.com)
which is the best way to do thisAgain: what have you tried so far?
I don't want the regular 404 page to display as it would to a real visitorOK, that's a legitimate concern if your 404 is designed to provide lots of good information and links for humans. Not that most robots will be distracted by links in the 404 page; typically they come in with a shopping list and will not be turned aside.
I was pretty active on this group many years ago, and I remember some pretty sharp people on the group regarding htaccess. I thought perhaps they might suggest a workable approach for those specific Wordpress calls.
- It is not appropriate to expect other members to write your code for you or to debug your entire project; Please don't expect other members to solve a problem you don't want to begin solving yourself.
- Don't get upset if someone has the answer but wants to provide you with resources and material to help you solve it on your own. After all, the most educational threads are those where members learn how to help themselves. Such threads also prove to be of most value the next time someone has a similar question.
[edited by: not2easy at 6:47 pm (utc) on Dec 23, 2019]
[edit reason] readability [/edit]
RewriteCond %{REQUEST_URI} file\.txt [NC]Never put something in a RewriteCond that can go in the body of a RewriteRule. mod_rewrite operates on a “two steps forward, one step back” system: FIRST it looks at the Rule itself and sees whether it applies to the request. If and only if the rule--including flags such [NS]--seems to apply, then and only then does it look at Conditions.
In future years, when mod_compat is no longer a part of Apache installs, it will probably provide a 500 response.Deny from allThis solution will provide a 403 response rather than the 301 response preferred.
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content|wp-login|wp-signup)/.*Is it possible you have misunderstood the significance of the leading ! (exclamation mark)? Also see above about Rule vs. Condition.
other folders may begin/contain with wp-“may contain” doesn’t matter, because you are using an opening anchor. Unless you de facto have filepaths that begin with wp- and then go on into other stuff (but why, for pity's sake? You said it isn't a WP site, so it is in your power to give appropriate directory names), there’s no need to spell out what-if-anything comes after the /wp- element.
RewriteRule ^/Do not use a leading / slash in a RewriteRule in .htaccess. The pattern will never match, and the rule won't execute.
A client may conceivably have their own folder with wp-something (a person's name - an article - whatever), even without WP installed, so I don't want to accidentally redirect.And this will be happening at the root of your own domain? Now I'm confused: Where is this RewriteRule to be located? In htaccess (which lives in some specific directory), or loose in the server's overall config file? Thanks to its wonky inheritance rules, mod_rewrite isn't usually the best bet for server-wide access controls.