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

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

Related

How do I make a custom 'File not found(404, 500)[http error code]'for my website

I have seen some websites have their own File not found pages with their logos.
I want such a page for my website.
Please tell me how to make a such type of page that shows the user if the file is not found.
First of all you need an HTML or PHP file to show when the 404 is occurs. Let's do a simple HTML file like this:
<h2> 404 Not Found! </h2>
<pre> Seems like the resource you're looking for is not found </pre>
Let's call this file 404.html.
Now you have to configure your webserver to redirect the not found request to your new HTML document.
If you're using Apache:
You can do it in different ways, the most common and recommended would be using .htaccess file, which allows you to override Apache configuration of a virtual host. However, to be able to use the .htaccess file in your virtual hosts you need to add allowOverride all inside your apache configuration.
Example of apache config:
<Directory /path/to/your/project>
Options FollowSymLinks
AllowOverride All # This is the needed option
Order allow,deny
Allow from all
Require all granted
</Directory>
So now you're able to put a .htaccess file in your project, so just create it and put this inside:
ErrorDocument 404 /path/to/404.html/document
In case you're using Nginx:
You must add the error page inside the nginx configuration. Follow this guide in order to achieve it:
https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-to-use-custom-error-pages-on-ubuntu-14-04
if you are using Apache server
in your project folder
consider your project is in
/mySampleProject
create .htaccess file in /mySampleProject if you don't have one.
simply add this line in your .htaccess
ErrorDocument 404 /yourCustom404Template.html
I will suggest you, use a framework of whatever programming language you want to build this application. They have in-built custom arrangements for such requirement e.g.
if( file_not_found Logic ){
show 404_page
}

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

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/

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

getting rid of .html using Apache on EC2

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.