Needs to ignore htaccess, when it contains wrong content - apache

I will explain, what I want to do.
We have an eshop, which generates htaccess file (server is Unix). Sometimes, it crashes during generating htaccess file, then whole site (frontend, admin, cron scripts => everything) is returning server error 500, due to not finished content in htaccess file.
And here is my question. I created script, which will regenerate htaccess file. But this script cant be anywhere in root (or subdirectories), because it will also return 500. Also subdomains are in root in subdirectory /_sub.
Is there any change to put it somewhere, where our customer will be able to execute it himself?
I cant use another domain; so I am asking, if there is any chance to ignore htaccess file, when it contants wrong content?
Thanks a lot.
EDITED:
I know, maybe one solution is here, but I think it will consume much more time, to code it all, but maybe not:
on another domain code script, which will connect through socket to ftp of eshop domain
it will delete htaccess file and recreate it to basic content
executing that cron, which will regenerate whole htaccess file
This should work, I think.

The question is, how layman your customer are. Thw options: 1) you make this from cron 2) you put this in a cgi out.
But AFAIK the best were, if only a testing script from cron runs, and this reconstructed only the bad htaccess files.

I know, maybe one solution is here, but I think it will consume much more time, to code it all, but maybe not:
on another domain code script, which will connect through socket to ftp of eshop domain
it will delete htaccess file and recreate it to basic content
executing that cron, which will regenerate whole htaccess file
This should work, I think.

Related

Rewrite Map Apache

We have a db which has from and to mappings of URL to redirect. Whenever someone wants a new redirect rule, instead of adding the rule in Apache we add them to the DB. A function does a lookup from the DB and does the redirect on each request.
We now want to replace that DB and instead use a new System which we have got in place. It is a content management system and hence we can host .txt files in it.
Ideally, i want to make a GET call to domain/rewrite.txt file in Apache and read the rules and if the request matches any of the rules then do a redirect.
I looked at the documentation here and found the only way to do this is to use the external rewriting program for this. Still things aren't making sense. Can anyone help how to do this?

How can I use an .htaccess file in Nginx?

I am currently migrating my website from Apache to nginx, but my .htaccess file is not working. My website is inside the /usr/share/nginx/html/mywebsite folder. How can I use .htaccess in my nginx server?
This is my .htaccess file:
RewriteEngine on
RewriteRule video/watch/([a-zA-Z0-9_#$*-]+)/?$ "videos-single.php?id=$1" [NC]
Nginx doesn't support .htaccess (see here: "You can’t do this. You shouldn’t. If you need .htaccess, you’re probably doing it wrong.").
You've two choices (as I know):
import your .htaccess to nginx.conf (maybe the htaccess to nginx converter helps you)
use authd-htpasswd (I didn't try it)
Disclosure: I am the author of htaccess for nginx, which is now open source software.
Over the past years, I created a plugin which implements htaccess behaviour into nginx, especially things like RewriteRule, Allow and Deny, which can be crucial for web security. The plugin is used in my own productive environments without a problem.
I totally share the point of efficiency and speed in nginx, and why they didn't implement htaccess.
However, think about it. You cannot make it worse if you're using nginx plus htaccess. You still keep the great performance of nginx, plus you can drive your legacy appliances effortlessly on one webserver.
This is not supported officially in nginx. If you need this kind of functionality you will need to use Apache or some other http server which supports it.
That said, the official nginx reasoning is flawed because it conflates what users want to do with the way it is done. For example, nginx could easily check the directories only every 10 seconds / minute or so, or it could use inotify and similar mechanisms. This would avoid the need to check it on every request... But knowing that doesn't help you. :)
You could get around this limitation by writing a script that would wait for nginx config files to appear and then copy them to /etc/nginx/conf.d/. However there might be some security implications - as there is no native support for .htaccess in nginx, there is also no support for limiting allowed configuration directives in config files. YMMV.
Using the config file is one option, but the cool thing about the .htaccess file is that it provided a way for a web developer to have some control over server settings without having root access to the server. There doesn't seem to be anything like this on nginx which is a real bummer.
I understand how the way it's setup on apache slows down response times, but hoped there could be an nginx way to do the same thing without the performance hit... At least a way to do rewrites with regex on urls if nothing else.
"Is there no nginx way to do bulk redirects using regular expressions that doesn't slow down response times."
Just edit your database with myphpmyadmin.
Open myphpmyadmin select your database then find your "yourprefix_Posts" table.
Open it then click the "Search" tab, then "Find and Replace".
Select "post_content" in the dropdown
In the "Find" field, type URL you want to change: "website.com/oldURL".
In the "Replace" field, type the new URL: "website.com/newURL".
(To use regular expression, tick the "Regular Expression" box.)
NOTE: You can test this out by simply leaving the "Replace" field blank.
ALWAYS BACKUP database before making changes. This might sound scary but its really not. Its super simple and can be used to quickly replace just about anbything.

How long does it take for .htaccess changes to take effect?

I have added url rewrite rules to my .htaccess file. Should I see these changes working right away?
Changes to .htaccess are immediate and do not require a restart. Normally, if you aren't seeing what you expect from .htaccess changes, you have a syntax error and should check Apache's logs for some idea of what's going on.
If you use redirect 301, clear browser cache.
NVM found it.
The .htaccess files follow the same syntax as the main configuration
files. Since .htaccess files are read on every request, changes made
in these files take immediate effect.
Apparently, some servers only check .htaccess files periodically. I've found this to be the case w/ our AWS host.
My source is only a comment below the answer here, but it matches my experiences and others seem to agree.

how do i use htaccess to make http requests work properly

I currently have css and javascript file calls (amongst other things) like the following:
href="/css/default.css"
src="/js/ui_control.js"
putting the preceding / in to make the files relative to the root.
This works great when my page is in the root of the domain.
However, I'm currently in the middle of transferring my site to a new hosting provider and as such have a temporary URL which is: HOST-IP/~username
As such, all file calls are trying to be called from HOST-IP/css/default.css etc instead of within the ~username sub-folder.
Of course I can wait until the domain name servers propagate but that's beside the point.
How would I go about writing a rule in the .htaccess file that would redirect all file calls that start with a /, from going to HOST-IP/FILE-CALL, and instead to go to HOST-IP/~USERNAME/FILE-CALL. ?
Any ideas?
I'd suggest changing the references in your HTML to the files to be relative, as this will work either in a sub folder or as the root of the domain.
This works great when my page is in the root of the domain. However, I'm currently in the middle of transferring my site to a new hosting provider and as such have a temporary URL which is: HOST-IP/~username
How would I go about writing a rule in the .htaccess file that would redirect all file calls that start with a /, from going to HOST-IP/FILE-CALL, and instead to go to HOST-IP/~USERNAME/FILE-CALL. ?
Unless you can put a .htaccess at HOST-IP/.htaccess on the new server, you can't do this with .htaccess. It sounds like you're on a shared host, so any approach that'd let you do this with .htaccess would allow you to hijack everyone else's site on the server.

Updating Files on Apache

I'm having trouble with my Apache Web Server. I have a folder (htdocs\images) where I have a number of images already in place. I can browse them and see them on my web server (and access them via HTML). I added a new image in there today, and went to browse to it, and it can't be found. I double and triple checked the path and everything. I even restarted Apache and that didn't seem to help.
I'm really confused as to what's going on here. Anybody have any suggestions?
Thank you.
Edit I just turned on the ability for the images directory to be listed, browsed to it (http://127.0.0.1/images/) and I was able to see all the previous images that were in the folder, but not the new one.
Turn directory indexes on for htdocs\images, remove (or move out of the way) any index.* files, and point your browser at http://yoursite/images/
That should give you a full listing of files in that directory. If the file you're looking for isn't there, then Apache is looking at a different directory than you think it is. You'll have to search your httpd.conf for clues -- DocumentRoot, Alias, AliasMatch, Redirect, RedirectMatch, RewriteRule -- there are probably dozens of apache directives that could be causing the web server to get its documents from somewhere other than where you think it's looking.
make sure the caSE and spelling are 100% correct.
There is not magic in programming (some may disagree:), so look for silly errors. Wrong server? Case of your letters? Wrong extension?
There's a chance it could be due to the cookies stored on your device. I would delete all cookies to the website you're working on before you refresh again