I have the following .htaccess which works wonderfully on a few websites of mine. However, I have uploaded it to another website (on the same host, different domain) and it is now defaulting to the error 404 page; which is displaying correctly.
Example URL: https://www.example.ca/resources-and-links/documents/
The .htaccess first checks to see if there is a actual .php file with first sub-directory, in the example resources-and-links.php. If it does exists it will serve up that page and break down the rest of the sub-directories into the query strings provided.
If the resources-and-link.php doesn't exsits, it directs it to the content.php to check it against pages in the database and serve it if the url matches one of that in the database. If it doesn't the content.php page shows a custom error 404 page.
This works on a sub-domain of said website https://sub.example.com which runs the cms system I built but not the root domain and as said before; shows the ErrorDocument instead. As well, I've used this same .htaccess on many of my other websites without a problem.
I used PHP to show that mod_rewrite is available so are there any reasons why it would not work on the root domain? Why is it skipping straight to the ErrorDocument and serving /404.php?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([^/]*)
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]*)(/([^/]*))?(/([^/]*))?(/([^/]*))?(/([^/]*)) /$1.php?ax=$3&do=$5&third=$7&fourth=$9 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/(.*)$ /content.php?url=$1/$2 [L]
Options +FollowSymLinks
ErrorDocument 404 /404.php
</IfModule>
Related
I am new to Apache and recently my developer passed me the website with htaccess to host on Apache 2.4 running on CentOS 7, the website is running on the Windows laptop but not on the server with the same htaccess.
The webpage works like this, when user hits the url at http://www.example.com, and click on the links, it will load the rest of the files from a folder called interface.
The original htaccess as follow:
RewriteEngine On
#this is for specific users
RewriteBase /v1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /v1/index.php?m=$1 [QSA,NC,L]
ErrorDocument 404 /v1/error.php?=error
However it only load the main page, and the rest will load as follow:
Not Found
The requested URL /example was not found on this server.
I have tried the recommendations on this page Generic htaccess redirect www to non-www
and this page .htaccess for cakephp
and this page Remove .php extension with .htaccess
however all of them not working as what we want.
Any way I can get it to work?
So I am making a new website, Link, but for some reason no folders have the full SSL icon. The top-level index.php file has the lock, but anything inside a folder, try /blog, has partial-ssl. It has the lock, but hides it because of the "Someone can change the look of this page" type error. Please help, as I do not know why this is hapenning. I do use cloudflare, and have set a http://qualexcraft.tk/* page rule to force https.
UPDATE: Now no folders or files have the full lock
For anyone interested, here is my htaccess file:
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
ErrorDocument 404 http://qualexcraft.tk/404
# Redirect www urls to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.qualexcraft\.tk [NC]
RewriteRule (.*) https://qualexcraft.tk/$1 [R=301,L]
Directory Structure:
index.php
404
index.php
old.php
assets
images
blog
allPosts
index.php
includes
headers
head.html
index.html
layout
footer.html
navbar.html
maps
mdl
[mdl files]
One of the reasons for the bahaviour is the following line in your htaccess file:
ErrorDocument 404 http://qualexcraft.tk/404
When the client requests for the image https://qualexcraft.tk/blog/images/android-desktop.png, a 302 redirection to http://qualexcraft.tk/404 is triggered. This page, in turn, has a permanent redirection set to https://qualexcraft.tk/404.
Now, as I asked in the comment, there is another rule which adds a trailing / in the URLs and redirects it to http://qualexcraft.tk/404/. This, lastly; redirects with the status code 301 to the secure page: https://qualexcraft.tk/404/.
The intermediate redirects to http pages is the root cause of your problem. The same occurs when someone visits the blog link on your website.
The request to https://qualexcraft.tk/blog gets redirected to http://qualexcraft.tk/blog/ and then to https://qualexcraft.tk/blog/.
After your changes to the website, the behaviour is still the same, except that the request is now for https://qualexcraft.tk/favicon.ico.
Try updating your htaccess to the following:
Options -MultiViews
DirectorySlash Off
ErrorDocument 404 https://qualexcraft.tk/404
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/?$ $1/index.php [L]
RewriteCond %{HTTP_HOST} ^www\.qualexcraft\.tk [NC]
RewriteRule (.*) https://qualexcraft.tk/$1 [R=301,L]
Add your favicon image to favicon.ico, to images/touch/ms-touch-icon-144x144-precomposed.png and to images/android-desktop.png. That is the only insecure content error that can be seen so once that is fixed you should be okay after clearing your browser cache.
In order to spot mixed content errors in the future, you can go to the console in Chrome (right click > Inspect Element > console) and your insecure content errors will be displayed there.
I'm using a php app called Yourls. It's a self-hosted url shortener and it's pretty great, I'm happy with its overall functionality. Due to the nature of its development however there isn't much in the way of support. Let's pretend the base url is af.to, where a shortened url would be af.to/goo that might redirect to whatever url is defined by 'goo'. The problem I'm facing is that if someone goes to af.to, they end up on a 403-Forbidden. I'd rather the client is redirected to a specific url instead. I have already picked up a plugin for Yourls which redirects to a url when a shortlink is not found or mis-typed, but this does not cover the base of af.to
I attempted to put in a 403 redirect in the .htaccess, but that broke the whole yourls script resulting in a 500 server error.
Current .htaccess looks like this:
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
Any help on what I need to do?
Thank you.
The RewriteCond blocks tell the RewriteRule to skip existing files / folders. When you go to http://af.to/, the root folder exists : no redirection. The apache server doesn't find any index.html (or index.php) file, isn't allowed to list the content of the folder, give up and returns a 403 Forbidden.
You can create the index.html file to show some content or you can add these lines to redirect to an other url :
# just after RewriteEngine On
RewriteRule ^/$ http://my-compagny.com/ [L,R=301]
for our client project we need to redirect all url calls to the matching .php file extension. this is done for SEO (google) ranking. avoiding indexing both url (with *.php and without)
we try to do this using the .htaccess file (shared host) but it seemed to only work for redirects that have a different url and not to the one that just adding the ".php" extension.
different url works
abc -> abc2.php
same url doesnt work
abc -> abc.php
here is our code sample:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co.uk
RewriteRule (.*) http://www.abc.co.uk/$1 [R=301,L]
# this doesn't work
Redirect 301 /test http://www.abc.co.uk/test.php
# this redirect works as the url is different
Redirect 301 /test-abc http://www.abc.co.uk/abc.php
we host with 1&1 shared host server.
php version is 5.5
I also tried this code that redirects but the page not loading now:
RewriteRule ^(.+)\.(.*)$ http://abc.co.uk/$1.php [R,NC]
also noticed url without file extension (.*|html) will not redirect
Try this .htaccess in DocumentRoot:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co\.uk$ [NC]
RewriteRule (.*) http://www.abc.co.uk/$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L,R=302]
This appears to be a 302 re-direct, which is not SEO freindly. I tested the above and it doesnt work, also, if it did I wonder what would happen to url's that did not require the .php extension on the URL.
Basically we're trying to get a 301 re-direct working, but it seems to be conflicting with the rules set up that contain the same url. (IE abc > abc.php)
I wonder if there could be some kind of Apache setting or even and unlikely a PHP setting? the 301's work on any other linux server that I try.
I am creating a custom web server that is designed to serve a single page, regardless of the request URL:
- www.example.com/
- www.example.com/spam/eggs/spam/?ID=eggs
- foo.example.com/
- ...
I am using a wildcard DNS entry to handle the subdomains, but I'm wondering about the best way to handle the page requests.
My first thought was simply to have no pages on the site and create a custom 404 page which was the page I wanted to serve, but I thought that losing an error page might have problems in the future, not to mention sending a 404 error to the client might have effects I am not aware of. Should I be using mod-rewrite instead?
How would you do this? 404, mod_rewrite, or?
I'll use this as an answer
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>