Whatever I do, .htaccess won't do the job - html

Working with .htacess file has always been a very frustrating experience for me. Someone please help.
This is what I want to achieve:
I am running Ubuntu 14.04.
Redirect my entire site (example.com) to a maintenance.html page.
Block everybody else except one IP, for example, I need to allow only 123.456.789.0
Here are my files:
Location of my index.html is /var/www/html
Location of my maintenance.html is /var/www/html
Location of my .htaccess file is /var/www/html
Contents of My .htaccess file:
#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
#301 Redirect Old File
Redirect 301 /index.html /maintenance.html
#Block users by IP
order allow,deny
deny from all
allow from 123.456.789.0
Please help me understand:
Is the location of each of the above files right? In what cases, the
page ends up in 500 internal server error?
What changes should I make in
/etc/apache2/apache.conf
/etc/apache2/sites-enabled/000-default.conf OR
/etc/apache2/sites-available/000-default.conf
Is is necessary to run a2enmod rewrite?
Should I add <IfModule mod_rewrite.c> and </IfModule> as header and footer in any of the above config files?
Sorry for too many questions, but I really want know it all this time.
Thanks in advance.

Is the location of each of the above files right? In what cases, the
page ends up in 500 internal server error?
A "500 Internal Server Error" message means there's an error and you're expected to check the server logs for the exact details. Apache will not display the error message to be seen by everyone.
What changes should I make
It depends on what the problem is. If the problem is "500 Internal Server Error" that means that we still don't know what the problem is.
Is is necessary to run a2enmod rewrite?
That command enables mod_rewrite. You need to enable it if it isn't enabled. You don't need to enable it if it's already enabled.
It's worth noting that this command is not part of official Apache distribution. Some Linux distros (namely Debian and derivatives) change third-party packages to match their configuration preferences, as in this case.
Should I add <IfModule mod_rewrite.c> and </IfModule> as header
and footer in any of the above config files?
As documentation explains, this block can be used to ignore directives when a given module is not installed. This can be useful for configuration templates to be distributed and optional features. In your case, it'll silently ignore your code if mod_rewrite is not available—you don't want that.
Last but not least:
order allow,deny
deny from all
allow from 123.456.789.0
... belongs to the old (and really hard to understand) Apache/2.2 syntax. If you are using Apache/2.4* you may want to try Require.
(*) Some distros hate bundling recent software but 2.4 has been around for several years

Thanks to #OlafDietsche and #ÁlvaroGonzález for this quick help. I am keeping their suggestions here so somebody like me will find it useful.
The problem is with my goals, not with the syntax. With their comments and answers, I came to know that my 2 goals were mutually contradicting ones.
I configured .htaccess to do both page-redirection and IP block. But if I am blocking (almost) everybody from accessing the site, page redirection makes no sense.
The required configuration in .htaccess is:
#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
#301 Redirect Old File
Redirect 301 /index.html /maintenance.html

Related

I'm getting a white page in a prod server with cake 3

My Cakephp 3 website is working well with Wamp, and when i transfer my website to my server i'm getting a white page.
My histing said that that the url rewriting is activated on my server, so i guess it's a problem with one of ny htaccess file.
http://www.project-heberg.fr (my domain)
My htaccess :
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName project-heberg.fr
http://www.project-heberg.fr/perles (where my cakephp 3 app is)
My htaccess :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
I think you may not have the requirements on your production server.
Requirements:
HTTP Server. For example: Apache. Having mod_rewrite is preferred, but by no means required.
PHP 5.4.16 or greater.
mbstring extension
intl extension
Please check or ask the host admin regarding the requirements on your prod server. I also faced the white page issue and i found this problem.
Hope this may help. Thanks

WordPress JSON API returns only 404 errors

I'm brand new to Wordpress and I would like to use the JSON API plugin.
So I put it in my plugin repository and I activated it but when I try to display the Json response by accessing the http://localhost/wordpress/wp-json/posts url I get a 404 error.
I'm probably missing something but according to the documentation it should be that simple. Any idea of what am I doing wrong?
It is an mod_rewrite issue.
Reason is one of these in your .htaccess:
not existing
wrong permissions
screwed up
Try the htaccess documentation on Wordpress for your .htaccess:
## BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Old question and answer, but for anyone coming here recently via search results (like me), /wp-json/posts should at least bring a JSON result (albeit still a 404 error), and /wp-json should list some available routes in JSON.
If it doesn't (e.g. it shows an Apache or other 404 error page), it's probably a permalinks issue
You have the wrong address. According to the documentation, the following are valid links:
Implicit mode examples:
http://www.example.org/?json=1
http://www.example.org/?p=47&json=1
http://www.example.org/tag/banana/?json=1
Explicit mode examples:
http://www.example.org/?json=get_recent_posts
http://www.example.org/?json=get_post&post_id=47
http://www.example.org/?json=get_tag_posts&tag_slug=banana
With user-friendly permalinks configured:
http://www.example.org/api/get_recent_posts/
http://www.example.org/api/get_post/?post_id=47
http://www.example.org/api/get_tag_posts/?tag_slug=banana
Source: https://wordpress.org/plugins/json-api/other_notes/
So in your case you should use http://localhost/wordpress/api/get_recent_posts/
/wp-json/elementor/v1/globals - 404 error
Just update the plugin if any update is available and deactivate it first and then re-activate it again. The problem will be solved.
When we activate the Elementor plugin then this plugin creates some essential files for editing, so when we will re-activate it then it will re-create all essential files and our problem will be solved. Thanks
It may depend on whether you have NGinx or Apache.
On some hosts with Plesk if Nginx is used, Plesk does not create or re-create the .htaccess file, but for WP it must be present.
If it's not there, just recreate it with the standard WP settings https://wordpress.org/support/article/htaccess/ as mentioned in one of the previous answers.
A workaround (insecure) is to create directories and files /wp-json/elementor/v1/globals with {} like Json standard, elementor will work the same, but I don't recommend doing that.

301 redirect for html files in one directory only to Custom Post Type in Wordpress

I am struggling to get my head around an htaccess rule to redirect requests for an html file to go to a custom post. I have looked on here and in other places and nearly got there.
I want to redirect ONLY mydomain.com/profiles/.html to mydomain.com/name_profile/
So mydomain.com/profiles/smith.html to mydomain.com/name_profiles/smith. There are some 900 html files to be redirected and they are all contained in this directory. Other html files in the domain I need to leave correctly associated.
I currently have
RedirectMatch 301 ^/([^/]+)/([^/.]+)\.html$ /$1/$2/
RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/.]+)\.html$ /$1/$2/$3/
But this redirects all html pages not just the ones in the profiles directory.
I am new at htaccess and have found several tutorials but none at a level I can understand, so any help is most welcome.
Place this rule just below RewriteEngine On line:
RewriteRule ^profiles/([^.]+)\.html$ /name_profiles/$1 [L,NC,R=301]
Use this rule:
RewriteEngine On
RewriteRule ^profiles/([^.]+)\.html/? name_profiles/$1 [DPI,L,R]
Or better, remove your RewriteMatch rules and replace with this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/.]+)\.html/? $1/$2 [DPI,L,R]
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)\.html/? $1/$2/$3 [DPI,L,R]
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

XAMPP only loads one of my pages

When I access my website using localhost/myfiles/index.html, my index file opens fine but if I want to click on the other pages/buttons, it doesn't work. The only way to open the other pages is if I do it manually by using localhost/myfiles/news.html and then only that pages works so I can't basically navigate my way around the website.
I had my website online yesterday and I know it works because I used it. I plan on making changes to my website and I need to be able to test them offline before I upload them to my online server. I just want to be able to navigate my site offline using XAMPP the same way that I would if it was uploaded on GoDaddy.
EDIT:
This is my .htaccess file
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Any suggestions ?
Seems you have .htaccess at your root and its not working properly.
in httpd-vhosts.conf
<Directory "PATH TO YOUR PROJECT ROOT">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Or update your .htaccess file
Without seeing your system it's hard to tell what's wrong but try the following (comment answer if these didn't work WITH log error messages)
[STOP your Apache server instance. Ensure it's not running!]
1) move apache server/install to a folder that has no long file names and spaces
2) check httpd.conf in install\conf folder and look for AccessFileName. If it's .htaccess change it to a file name windows accepts (e.g. conf.htaccess)
3) double-check that your htaccess file gets read: add some uninterpretable garbage to it and start server: you should get an Error 500. If you don't, file is not getting read, re-visit httpd.conf file (if that looks OK, check if this is the only file which defines htaccess and it's location and it does at one place -within the file- only; also check if both httpd.conf and htaccess files are accessible: not encrypted, file access rights are not limited, drive/path available -and no long folder path and file names-)
STOP Apache again, then go on:
4) If you have IIS too on your system, stop it (uninstall it too if you can) from services.msc
5) Add the following to the top of your valid htaccess file:
RewriteEngine On
RewriteLog "/path/logs/rewrite.log" #make sure path is there!
RewriteLogLevel 9
6) Empty your [apache]\logs folder (if you use another folder, then that one :)
7) Check the following entries are set and correct:
Action application/x-httpd-php "c:/your-php5-path/php-cgi.exe"
LoadModule php5_module "c:/your-php5-path/php5apache2.dll"
LoadModule rewrite_module modules/mod_rewrite.so
Avoid long path names and spaces in folder names for phpX install too!
8) START apache server
You can do all the steps above or go one-by-one, your call. But at the end of the day make sure you tried everything above!
If system still blows up and you can't fix it, copy&paste error message(s) from log folder for further assistance

Server side changes in Angular html5 mode

I was wondering if any one can tell me what are the server side changes which needs to be done when you change your mode in to html5 in Angular js. because When I did tried to change the it to html 5 mode I was unable to go in to my inner html pages. in Angular API it says that suers need to do a server side changes as well.
what are the server side changes
do we need to do any other changes as well ?
You should distinguish two type of calls:
HTML calls from the browser. These should be all remapped to serve your index or whatever.
JSON API requests. These should pass through to your app server.
How to distinguish between these two types of calls, and how to remap the former, depends strongly on your setup.
If you are using nginx, for instance, a combination of checking $http_accept to be application/json (see http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requests and http://wiki.nginx.org/HttpCoreModule#.24http_HEADER) and rewrite (http://wiki.nginx.org/HttpRewriteModule#rewrite) you can achieve what you want.
You need to setup your server to rewrite everything to index.html per: https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode ...
<VirtualHost *:80>
ServerName my-app
DocumentRoot /path/to/app
<Directory /path/to/app>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
My app has params passed to a controller (via ui-router) so before html5mode I would goto
www.blah.com/angapp/#/myUIrouterController?param_x=1&param_y=2
Guessing browsers know that /#/ folder path part should serve index.html.
Now that # would be gone with html5mode, the server by default doesn't know to serve the index.html for that folder since the url will just be:
www.blah.com/angapp/myUIrouterController?param_x=1&param_y=2
myUIRouterController isn't a real file so the server would just serve a 404, Hence why I think the rewrites are needed so it knows to send everything to index.html (so above in combo with the <base> tag should work... note: requireBase is optional but heard it helps older browsers like IE9 maybe).