So I recently purchased a domain. I know how to make websites, so I uploaded a website that I made onto that domain. The only problem is is that it sends me to my index and then I have to click on some folders to actually get to see my website on my screen. I know what .htaccess is and does, but I'm not sure how to use it.
What I want is that when I go to www.mydomain.com it should open up my home.php file from the website that I made. This is my file order:
project/PHP/home.php
I'm not sure if I've given enough information, but I hope someone can help me out here.
As correctly written by #JimL in the comments above we would recommend that you simply replace home.php to index.php, since that is the default setting really all http servers are configured to use for the index document.
You can however change that, even if you can only use .htaccess style files:
DirectoryIndex home.php
That said I still would recommend to rename the index file instead. .htaccess style files should be avoided whenever possible. They are notoriously error prone, hard to debug and they really slow the http server down, often without reason. They are only offered for cases where you really need to do some configuration tweaks but do not have control over the http servers host configuration. That is for example often the case when using a cheap web space provider.
Considering the additional information you gave in the comment below you could also try to rewrite all requests to point to the php scripts inside that folder project/php. For that you can place such rewriting rules inside a .htaccess style file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/project/php
RewriteRule ^(.*)$ project/php/$1 [L,QSA]
If you also have to handle requests that require different rewriting then obviously you need additional rules.
But as already said in the comments below this is painful, slows the server down and makes things harder to debug.
Put your files in the public_html folder, or /var/www, you don't need the .htacces to do this.
Related
I'm using XAMPP 7.2.6 on Windows and my root web folder is htdocs. I have the mod_rewrite module installed in Apache.
I have a website project in C:/xampp/htdocs/xyz
I want to use mod_rewrite to rewrite the URL of the root directory and index.php to a custom URL in the browser - index.php?action=viewFrontPage&pageId=2
.htaccess file
RewriteEngine on
RewriteRule ^/index.php? /index.php?action=viewFrontPage&pageId=2
This does nothing and I can't figure it out.
There are a few issues with your approach, but I will point out the first and most obvious:
Your current configuration
RewriteEngine on
RewriteRule ^/index.php? /index.php?action=viewFrontPage&pageId=2
does not express what you expect it to express and in fact that rule will never get applied. Why? Check the documentation! That is where you can read that the pattern will get matched against a relative path of a rewriting rule is implemented in a dynamic configuration file (".htaccess"). You try to match an absolute path.
That is why I would suggest this as a first step towards your goal (which is actually unclear from your question):
RewriteEngine on
RewriteRule ^/?index\.php$ /index.php?action=viewFrontPage&pageId=2 [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
That adapted rule will not work. This brings us to step 2... The issue you now face is that you have created an endless rewriting loop. You redirect /index.php to /index.php... not a good idea for obvious reasons. So you need to prevent such a loop, but for that you will need to be able to tell how the server should distinguish between the two situation. Are you able to?
This might be a start, but you will certainly need to adapt it:
RewriteEngine on
RewriteCond %{QUERY_STRING} !^action=viewFrontPage&pageId=2$
RewriteRule ^/?index\.php$ /index.php?action=viewFrontPage&pageId=2 [END]
Also also you wrote that you want to rewrite the "root directory", but it is unclear what you actually mean by that...
I'm currently coding an Admin Panel, and for the sake of organization, I've made it so that all of the pages are located in a sub-directory instead of the root directory and I want to access these pages in the sub-directory from the root link. For example, the index.php is in the root directory and is accessible from:
http://localhost/index.php
But the rest of the pages are located in a sub-directory like so:
//What the current link is
http://localhost/pages/page.php
//What I want the link to be
http://localhost/page.php
Some of my pages are in sub-directories of the sub-directory like so:
//What the current link is
http://localhost/pages/page1/page.php
//What I want the link to be
http://localhost/page1/page.php
As you can see, I simply want to eliminate the primary sub-directory from the link, which is pages.
I've looked around on the internet and from what I can tell, this is achievable through .htacess but I couldn't find anything that worked.
You certainly won't find a perfect solution you can simply copy and paste to solve all your tasks. That expectation is a strange on. Instead you are expected to actually understand how the tools you want to use work and to be able to find your own solution. For that there really are enough good examples here on SO and in addition there is an __excellent_ documentation of apache's rewriting module available with just two mouse clicks.
Anyway, her is a suggestion, you still may have to tweak it to your situation:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /pages/$1 [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
I'm in the process of working on an error system for my site (i.e., if MySQL encounters an error, it sends them to an error page). I'm wondering, is it possible to use a "/" instead of "?err=" for a URL?
What I'd like to do is have people sent to the url "/error/404/" but display on page the content at url "/error?err=404". Is there a way to do this with HTAccess, or something of the sort?
My current way is with lots of files and iframes, and it gets really annoying when you have to update one tiny little thing.
Thanks!
What you are looking for is url rewriting. You can set it up using an .htaccess file, given that your installation of apache has mod_rewrite enabled (if not, check this question).
Here is a nice tutorial on how to do it.
Have a try with this:
RewriteEngine on
RewriteRule ^error/(\d+)$ error?err=$1 [L,QSA]
This should not end in a redirection loop, since this requires a trailing number in the URI.
Note that I removed your leading slashes from both the pattern and the result. .htaccess style files work on relative paths.
In general you should always prefer to place such rules inside your http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and the really slow the server down. They are only available as a last option for users who do not have access to the host configuration, for example when using a cheap hosting provider.
I have a little problem. I have img with wrong source.
The source is :
<img src="http://mywebsite.com/mk/thumbnail.php?file=file-dtik.jpg&size=article_medium" align="left" alt="image">
And the source need to be changed to
<img src="http://mywebsite.com/thumbnail.php?file=file-dtik.jpg&size=article_medium" align="left" alt="image">
Without the MK folder after the website.
I need to change this via .htaccess ? What is the rewrite rule for this ?
EDIT :
Also another problem. THere is some .pdf links which are not linking correctly i need them also to re-link with .htaccess.
http://207.58.130.89/~tempuser/mk/files.php?force&file=documents/documents/file.pdf
This is the current link and i need redirect when someone click to :
www.mydomain.com/mk/files.php?force&file=documents/documents/file.pdf
Thanks.
In general you should always prefer to code such rewriting rules in the server configuration instead of using .htaccess style files. The reason is that such files are notoriously error prone, hard to debug and really slow the server down.
Rules example for the server configuration:
RewriteEngine on
RewriteRule ^/mk/thumbnail\.php$ /thumbnail.php [L,QSA]
There are only two exceptions to this rule:
you do not have access to the server configuration
your code needs to make dynamic changes to the set of rewriting rules
Rules example for .htaccess style files:
(note that you first have to enable usage of such files inside the server configuration)
RewriteEngine on
RewriteRule ^mk/thumbnail\.php$ thumbnail.php [L,QSA]
For this rule to work the .htaccess style file must be located inside the document root at top level, where the mk folder exists, physically or virtual.
Disclaimer: I did not test-run this, sorry if there is some typo...
In general you should always consult the excellent documentation for this stuff!
In case you have problems getting such rewriting rules to work, there are three basic places where to start debugging:
if you have access to the server log files, then that is where you should start: first you should see error entries when the rewritten request does not point to the expected location or otherwise fails
even more elegant is to use the builtin rewrite logging, if you have control about the server configuration. It allows to gain really detailed insight into what is (or is not) going on inside the rewriting engine. See the documentation ink I posted above for details.
you can specify a temporary and additional "external redirection flag" which allows you to see the url the request is written to in your browsers url bar. For this just replace the flags [L,QSA] b a [L,QSA,R=301] which results in the server sending an external redirection header on http level instead of performing a silent and internal rewriting. This often helps to understand what is going on.
In kohana framework, in .htaccess file is writen
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
Ok, But why needed this security in each php file :
<?php defined('SYSPATH') or die('No direct access allowed.');
?
attacker alredy can not open any .php file directly right? (becuase reason is protecte from .htaccess RewriteRule)
Simpy: It's a fallback. Thats all, but I need more characters, to publish this answer
It seems like the developers wanted to make sure no files can be accessed, no matter if the .htaccess works or not (i.e. disabled mod_rewrite).
But for files that only contain class definitions or return/define configuration arrays it is pretty useless anyway, since they don't output anything.
You can't be sure - from a framework developer point of view - that the webserver, that your product will be run with, is correctly set up (e.g. .htaccess/RewriteEngine not enabled by AllowOverride or no mod_rewrite ...).
this kind of « second check » is there to ensure that the framework won't leak sensitive data even on badly set up hosting.
.htaccess works only on Apache with mod_rewrite enabled. If the server does not meet any of these condition those SYSPATH checks comes in handly.
Note: not every user can use Aapche as web-server. And Not every user has access to .htaccess.
There are other alternatives nowadays. Like, nginx.