getting rid of .html using Apache on EC2 - html

Is there a simple way to remove the .html from a URL namespace? For example, if I have www.mywebsite.com/special.html how can I make this www.mywebsite.com/special
Again, I'm using Apache on an Amazon EC2 instance. Thanks in advance!

Put this in your .htaccess file:
RewriteEngine On
RewriteRule ^(.+)\.html$ /$1
Make sure that mod_rewrite is installed and AllowOverride includes FileInfo.
Edit on how to get mod_rewrite working:
Usually, mod_rewrite already comes with apache, but sometimes needs to be enabled by the command
a2enmod rewrite
The AllowOverride directive sets permissions for .htaccess files. For mod_rewrite, FileInfo is required. To set this, change in your apache config file (usually /etc/httpd/conf/httpd.conf) the line
AllowOverride None
to
AllowOverride FileInfo
or
AllowOverride All

Make a directory named special and put an index.html inside of it with the markup from special.html.

Related

How can I remove /public and the name from URLs in Bolt?

I'm working on a small site and I've got a question.
How can I remove the /public and the singular name from url? However for the entries I want to keep the first segment.
How do I do that? Because when I try to rewrite it in the htaccess the assets break (images etc)
You need to point your server to the /public directory. Here is what the official documentation is saying:
Only the 'public' folder needs to be accessible in the browser. After the first installation this folder is named public/ but as you read on, you will see that you can rename it to www/ or whatever your web server requires. To do this, configure your webserver to use the public/ folder as the web root. For more information about this, see the pages on configuring Apache or Nginx.
Source: https://docs.bolt.cm/3.2/installation/install-command-line
The directive for Apaches VirtualHost is (in your example domaincom.conf in /etc/apache/sites-enabled:
<VirtualHost *:80>
DocumentRoot /your-bolt-directory/public
</VirtualHost>
And for Nginx:
location / {
root /your-bolt-directory/public;
}
Use .htaccess file to rewrite your URL's:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/page [L]
</IfModule>
The public part of the url should not be visible if you have your Apache virtualhost setup correctly.
Ordinarily, all you need to do here is point the DocumentRoot setting to the public directory, so for example if your setup currently looks like this:
DocumentRoot /home/mysite
You adjust it to:
DocumentRoot /home/mysite/public
You may also need to do the same for any <Directory /home/mysite > commands that are in your config too.
Once you have amended, restart Apache and http://yourdomain.com/ should load up the homepage and http://yourdomain.com/bolt take you to the backend.

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

Apache (.htaccess and mod_rewrite) url rewrite/redirection not working :404 page not found error

I just installed the mod_rewrite module on my server and am trying to test it out by directing all urls to a webpage good.html
this is my .htaccess file:
RewriteEngine on
RewriteRule .* good.html
however I keep getting a 404 page not found message?
I've checked my httpd.conf file and ran phpinfo() and the module seems to have loaded fine. Any ideas?
Also I am working on apache 2.0.58
I've got the same problem. Everything looks fine, but I had to change two configurations on httpd.conf (or any other site config loaded).
Change to:
Options FollowSymLinks
AllowOverride All
These options are inside Directory tag. If you do not set AllowOverride All, .htaccess does not take effect.
Eder

Wordpress JsonAPI - /wp-json/ was not found on this server

I am using the following plugin Json Rest API.
To test the plugin the documentation states that I should just use:
$ curl -i http://testpress-maxximus.rhcloud.com/wp-json/
HTTP/1.1 404 Not Found
Date: Sat, 24 May 2014 07:01:21 GMT
Server: Apache/2.2.15 (Red Hat)
Content-Length: 303
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /wp-json/ was not found on this server.</p>
<hr>
<address>Apache/2.2.15 (Red Hat) Server at testpress-maxximus.rhcloud.com Port 8
0</address>
</body></html>
As you can see nothing is found by the URL. Any recommendations if there is a problem with the API or wordpress?
I appreciate your reply
The current version of REST api for sites with pretty permalinks not enabled, the url
yoursite.com/?rest_route=/
will work .
The WordPress JSON API depends on pretty permalinks, make sure you have them enabled for the site.
In my case, I got this error after installing/configuring apache2 on my local linux machine. I finally found the error to be cause by the rewrite module not being enabled which I fixed using,
sudo a2enmod rewrite
as well as ensuring that my apache2.conf file (located in the folder /etc/apache2) has its<Directory> directive 'AllowOverride' set to all rather than none, from
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
to
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
then I restarted apache2 service and the problem was resolved.
I have faced this issue several times . The solution is this :
Login into your Wordpress site: example.com/wp-admin
Then click on settings
Then click on permalinks
Then set permalinks to "post-name"
Save Changes
Sometimes the solution is crazy and easy! Go to the permalink settings by moving to Admin -> Settings -> Permalinks...then just hit Save Changes without doing anything else! This refreshes the memory of WordPress.
Why is that? For a situation I had before, I had changed the main website URL so I had to refresh the permalinks as well.
I had this same issue and wanted to post my solution in case anyone else comes across this answer and the other answers don't solve the issue, as this happened with me.
In my case I didn't have a .htaccess file with Wordpress' default mod_rewrite rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This solved the issue for me. Per the documentation:
WordPress uses this file to manipulate how Apache serves files from
its root directory, and subdirectories thereof. Most notably, WP
modifies this file to be able to handle pretty permalinks.
I was running WP on a local dev environment in a subdomain of localhost (eg mysite.localhost:8888)
The solution for me was to update the virtual host config in httpd-vhosts.conf to set directory options, similarly to Aurovrata's answer:
<VirtualHost *:8888>
ServerName mysite.localhost
DocumentRoot "/Users/myusername/mysite"
<Directory /Users/myusername/mysite>
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
Faced a similar issue, turns out that Apache's mod_rewrite module wasn't enabled. Worked fine after enabling it.
For me, this issue was due to the WP site being developed at the root of a staging URL (ie example.com) but when put live it was installed in a sub-directory (ie example.org/wp)
Before I could make the suggestion from this comment work I had to chmod 664 .htaccess to make it writable by Wordpress. I then resaved the Permalinks as suggested and Wordpress updated the RewriteBase in .htaccess to /wp
If you have correctly installed the plugin, be sure to flush the rewrite rules.
This can be accomplished with the wp-cli: http://wp-cli.org/commands/rewrite/flush/

Can't block access to a file using htaccess

I want to block a file config.json from access, so I put .htaccess file like this:
RewriteEngine on
RewriteRule config.json - [F]
In directory ~/projects/bush but when I access localhost/projects/bush/config.json I still see the content of the file, what wrongs here?
Check the web server's configuration (httpd.conf) for AllowOverride None as it disables reading of .htaccess for the directories it applies to. It is often done to increase performance.
try:
RewriteRule ^/?config\.json$ - [F,L]
This probably will work.