Rewrite rules for pages that do not exist in the database - apache

As of now, my htaccess looks like this
RewriteEngine On
RewriteCond %{QUERY_STRING} ArticleID=([^&]+)
RewriteRule ^article.cfm$ /articles.php?id=%1 [R=301,L]
RewriteRule ^([a-zA-Z0-9-]+)$ page.php?slug=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ page.php?slug=$1
ErrorDocument 404 "<H1>Page not found</H1>"
The first 3 lines allow me to redirect users who are access articles through .cfm to .php instead
For the 4th and 5th lines, my website allows creation of pages through a WYSIWYG editor and allow users to access it. The actual url will look like www.website.com/page.php?slug=homepage but after the rewrite rule, it will look like www.website.com/homepage
However, I was having problems where my admin pages such as www.website.com/admin/ was regarded as one of the page so I added the below codes and it works
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
But I'm still having a couple of problems.
If you access an invalid page such as www.website.com/hello/ when there is no such page created by the users at all, it will still show page.php but with empty contents, how do i redirect it to a 404 page?
How do I secure directories which does not have an index.php page? such as my images folder, includes folder, javascript folders etc?
Does my entire htaccess looks right at this current point?
Thank you guys so much for helping! I'm not good with mod_rewrite so I will appreciate your help!

If you access an invalid page such as www.website.com/hello/ when
there is no such page created by the users at all, it will still show
page.php but with empty contents, how do i redirect it to a 404 page?
You should do this with PHP. If there's no content included, use header() to redirect to a 404.
How do I secure directories which does not have an index.php page?
such as my images folder, includes folder, javascript folders etc?
Add this to your .htaccess: Options -Indexes
Does my entire htaccess looks right at this current point?
Looks fine.

Related

How to prevent user to access css files and images via the url

I am using .htaccess to prevent user to access files through url.
The line
RewriteRule ^frontend/assets/css/(.+) index [L,R]
works fine. It prevents users to access css files.
However,
RewriteRule ^frontend/assets/images/(.+) index [L,R] //Images
as well as
RewriteRule ^frontend/assets/js/(.+) index [L,R] //Javascript files
do not work.
I have tried multiple solutions but still I can access js and images (png and svg) files from the url.
Is there a way to handle this ?
Please following rules at the top of your htaccess rules file. You need not to create 3 separate rules for 3 conditions, you can use regex here and can do a 401 redirect for all of 3 strings(js/images/css) in a single rule itself.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^frontend/assets/(?:images|css|js) - [R=401,NC,L]
actually, this helped me: Allow/deny image hotlinking with .htaccess
small video with the same example: https://www.youtube.com/watch?v=PsbpuH_e12E
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https://(subdomain\.)?domain\.com [NC]
RewriteRule \.(gif|jpeg|png|jpg|js|css)$ - [F,NC,L]

htaccess | redirect 301 all files to homepage

Sorry to be asking yet another htaccess question but I have searched at length and can't crack it.
I have changed my website to a single page, www.mysite.com/index.html. Previously there were subdirectories and sub-subdirectories, all with a combination of html and other files in them.
I now want all file requests to be directed to the homepage, and for the homepage to display in the browser bar. Eventually I want to remove the old directories and images, but I'm assuming for now that Google needs me to keep them in order to transfer any ranking (is this right?).
There is just one exception to the rule that all traffic should go to www.mysite.com. I have bought another domain www.mysite2.com for SEO reasons. Currently this site has no hosting and is set to redirect to www.mysite.com. If users find www.mysite2.com, I would like them to see content from www.mysite.com but display the domain name www.mysite2.com.
This is the htaccess code I am currently using:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^?]*)$ /index.html?path=$1 [NC]
Options -Indexes
IndexIgnore *
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule ^(.*) http://mysite2.com/$1 [P]
I have several questions/problems:
1) the www.mysite2.com redirect threw a 404 file not found error when i tried to access www.mysite.com. (I am having to test in live production... not great).
2) typing in www.mysite.com/products/ (former directory, still existing) takes me to www.mysite.com/// - it works but is unsightly.
3) typing in www.mysite.com/products/subdir/file.html still takes me to that original file
4) typing in www.mysite.com/lsdhfskdhf (i.e. a non-existent location) displays the homepage but continues to show the string.
5) typing in www.mysite.com/products/ksjdhfkdsjfh (i.e. non-existent location) displays the homepage but without any css formatting (!).
6) Should i be using Redirect 301 instead of RewriteRule? I heard that Google only likes 301 redirect.
I am a bit stuck and would welcome any help! Even if there is a comprehensive guide somewhere to whatever language apache is using in its directories that would be good. Lots of sites claim to this but i haven't found a good one.
Apologies for length.
Thanks
Emma

Htaccess rewrite url to hash anchor on homepage

I need to create an htaccess rule for a website i built in codeigniter for an apache web server.
I basically have a one page website which displays sections of the site on the the homepage.
for example...
http://www.example.com/sales
http://www.example.com/portfolio
...all appear as sections on the homepage so it display on one page only
My plan is to use anchor tags and hashes to direct the user to the right area on the homepage but I can't figure out the correct htaccess rule to modify the url to redirect to the homepage and to the correct anchor point
So my prefered method to do this would be to a rule to redirects to the homepage like so...
**http://www.example.com/sales**
redirects to
**http://www.example.com/#sales**
And if the url has more directories I need them to go to the highest parent on the home page
for example...
**http://www.example.com/sales/future/page1**
redirects to
**http://www.example.com/#sales**
I would also need to make sure that i hide the index.php that displays at the beginning of the segment!
Any ideas or alternative suggestion to get this working would be greatly appriciated
Kind Regards
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/|$) /#$1 [L,NE,R=302]

How to rewrite all 404's to index.php using cPanel URLs?

I'm trying to rewrite all 404's to index.php where I use PHP's parse_url() to determine which file to include (e.g. about-us.php, contact-us.php) and I'm getting some really weird results.
I'm working on a 'dev' URL automatically created by cPanel:
http://xxx.xxx.xxx.xxx/~mySite/
Current Method
My .htaccess file contains the following:
RewriteEngine on
RewriteBase /~mySite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]+)$ index.php
And the results are a mixed bag:
xxx.xxx.xxx.xxx/~mySite/contact-us renders just fine.
xxx.xxx.xxx.xxx/~mySite/contact-us/phone throws a 404 that isn't caught by mod_rewrite.
xxx.xxx.xxx.xxx/~mySite/about-us rewrites to the server root (xxx.xxx.xxx.xxx/index.php).
Previously Tried
ErrorDocument 404 /~mySite/index.php
And I get the similar results, except for the following:
xxx.xxx.xxx.xxx/~mySite/contact-us/phone rewrites to index.php but all my CSS and JS includes are off because they're trying to load relative to /~mySite/contact-us instead of /~mySite.
Any help? I'm going out of my mind. Especially the fact that contact-us works fine, but about-us doesn't?
First don't use cPanel preview. That is not a good way to view your dev site. Who knows how it will affect the rules. Also control panels do weird things anyway.
Preview your site using your real site domain name. You can do that my modifying your HOST file on your computer so that only you can view it by the domain name. This little guide will show you how to edit it. It takes like 2 minutes.
Then that should help to check things better.
Most likely why the error document doesn't work because ~mySite is most likely not your document root. That is typically how cpanel does it's preview links. So your real error document should probably be as Marc B stated.
ErrorDocument 404 /index.php
If you want a mod_rewrite solution, this should also work. But I wouldn't use both.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [R=301,L]
On a side note, I think it's usually good to use a 404 page so that users know that the requested page is not a real page instead of some people thinking something is just wrong with your site because it redirects to the home page. Like facebook, it gives you a big thumbs up with a bandage on it saying it the page is not available. I've seen many custom 404 pages that were pretty clever so maybe it might be time to just get creative.

301 redirect html to index.php file from existing .htaccess file

I trying to create some 301 redirects for a friend. His old site was a static site built with a few html pages.
He got a new site built by a developer and would like me to have his old urls redirected to the equivalent page on his new site. If the new site does not have an equivalent page, he would like to have it redirect to the front page.
The old site only had 9 or 10 pages so writing a line in the .htaccess file for each redirect is not a problem for me.
The problem is I have tried everything possible to get a redirect to work using the .htaccess file created by the developer of his new site, with no luck.
The current .htaccess file contains the following lines
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /~example.com/index.php/$1
I have tried all sorts of stuff found on on this site and on google. for example, I would be happy if I could get this to work
RedirectMatch 301 (.*)\.html$ $ /~example.com/index.php/$1
I tried putting that line above the existing RewriteCond but below the existing RewriteEngine line and also tried putting it at the bottom below all the existing lines, but no go. every example I have found does not work. I keep getting a 404 page no matter what I do.
I am a front end guy with no knowledge of Apache and small knowledge of frameworks outside of Drupal and wordpress but It appears that this site is a custom CMS built with the zend framework. The site has an admin interface for creating new pages but has very little options and definitely no option to create redirects. Any ideas?
oh, one more thing. I know that I am in the web directory because if I remove the index.php file the base url returns a 404.
RewriteEngine on
RedirectMatch 301 /(.*?)\.html$ /~example.com/index.php/$1
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /~example.com/index.php/$1