Okay...My title is a bit of an exaggeration...
My site is built in PHP, and all the files I'm trying to "require_once" aren't being loaded. The only file I've changed is my .htaccess file. I don't know a thing about .htaccess and what I have is purely from searching the web. What is wrong? Thanks for any help in advance.
RewriteEngine on
<Files 403.shtml>
order allow,deny
allow from all
</Files>
ReWriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule !index.php index.php [L]
Also, if I comment out the bottom two lines, my site works great.
Well, require_once has nothing to do with .htaccess file: it's a PHP directive, not an Apache one. You have to set correctly the include_path for your files and make sure these directories and files are reachable (i.e., with correct privileges set on them).
If you show the error message you got from failed require, it'd be much more simple to give you a specific advice on how to fix it.
UPDATE If what you need is redirecting all the non-AJAX requests for .php files into index.php, your .htaccess should like this:
RewriteEngine on
RewriteCond %{HTTP:x-requested-with} ^XMLHttpRequest$
RewriteRule . - [L]
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule .php$ index.php
This basically means the following: "all AJAX requests - go for what you need, all non-AJAX requests IF you're not going for some directory and are ended with .php - go for index.php instead".
Without checking for .php (or some similar check) you will redirect to index.php all the script loading procedures; and, unless you do it from some external CDN, it's not what would work in your case. )
Try changing the last two lines to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
If you want your URL's to look something like this (you probably do):
http://yoursite.com/some/path/somewhere
then change the last line to:
RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2
If that's what you want to achieve, ensure that if you're trying to go to:
http://yoursite.com/about
That there isn't actually a folder called about, this line:
RewriteCond %{REQUEST_FILENAME} !-d
Checks to see if a folder with the name "about" exists, if it does, then the page will not redirect, the same goes for files, say you go to:
http://yoursite.com/about.html
If about.html actually exists then the page will not redirect.
Hope that makes sense.
If you need more information, http://jrgns.net/content/redirect_request_to_index seems to be fairly succinct and helpful.
Related
I have recently installed the latest LimeSurvey, and while it has many features I like, I only really intend on having one survey available at a survey.example.com subdomain.
I want to skip the page that states "The following surveys are available:" which is the "survey.example.com/index.php". and go straight to the survey, which is "survey.example.com/index.php/311746?lang=en"
I tried setting a DirectoryIndex in .htaccess but that didn't do anything
DirectoryIndex index.php/311746?lang=en
I've tried playing with mod_rewrite, but LimeSurvey itself already has conditions set up, so whatever I tend to do breaks theirs (likely since they're already rewriting index.php).
<IfModule mod_rewrite.c>
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php
</IfModule>
I was going to try a redirect, but the index.php is more than just the index, so I don't want to change that too much.
I've tried searching around but I haven't had much luck.
Then why not just rewrite the main rule of the survey. Instead of rewriting it to the main index.php which you don't want to do, write it directly to the survey you want. You should be able to use this in your htaccess to go directly to the survey.
<IfModule mod_rewrite.c>
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to the survey
RewriteRule . index.php/311746?lang=en [R=301,L]
</IfModule>
Let me know if this solves your issue
Hi, so this may be asked elsewhere but I have searched and come up with irrelevant results.
I clearly don't know what to search for exactly.
I'm just trying to rewrite everything after a certain directory to that directories index.php.
Here is an example of the URL a visitor would SEE
website.com/search/location/United%20States
And I would like that URL to be rewritten server-side so that it loads website.com/search/location/index.php
(not a 301 redirect)
I would like the Url to stay the same but load the index.php script (to include United%20States so this can be passed to PHP to determine what the location is and if it is legitimate etc.).
Sorry I know that this will be somewhere already but I just can't find it
I have some code already but it is buggy and seems to choose when it wants to work and also sometimes uses location/index.php/United%20States which is not what I want.
Put this code in your htaccess (which has to be in your root folder)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/location/.+$ /search/location/index.php [L]
If you have Apache web server, make sure you have mod-rewrite enabled and put .htaccess file into your WEBROOT/search/location directory. Put this into .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
This will internally redirect all requests, where file or directory does not exists, to index.php.
You could also put .htaccess file into your WEBROOT directory and write this into to:
RewriteRule ^search/location/.* /search/location/index.php
Hope this helps.
I have a question about using multiple .htaccess files - I couldn't find the answer to this after looking elsewhere on stackoverflow, so I hope you guys can help.
I currently have one .htaccess file in the root of my site, which performs a simple url rewrite:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [L]
ErrorDocument 404 /index.php
I'm currently working on the second phase of development of this site, and I've made a replica in a subfolder (e.g. www.abcdef.com/new/). The trouble is, at the moment if I click a link on this replica site, it redirects me to the root, original page, whereas I want it to go to the equivalent page in the new/ folder. I've put another .htaccess file in this new/ folder, which however doesn't have any noticeable effect:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /new/index.php?url=$1 [L]
ErrorDocument 404 /index.php
So my question is: is it permissible to have another .htaccess file in a subfolder like this? And if so, why aren't the above lines working?
Thanks in advance for any ideas on this!
It's possible to have multiple .htaccess files, and the system is designed to work the way you want it to.
You're setting RewriteBase, which explicitly sets the base URL-path (not filesystem directory path!) for per-directory rewrites.
So it seems like your requests would be rewritten to /new/new/index.php, a path and directory which probably doesn't exist on your filesystem (thus not meeting your RewriteConds) and such is being redirected to your /index.php 404.
As a test, perhaps try changing the ErrorDocument to:
ErrorDocument 404 /new/index.php
If you see rewritten calls go to this then it might indeed be your RewriteBase.
You say
The trouble is, at the moment if I click a link on this replica site,
it redirects me to the root, original page, whereas I want it to go to
the equivalent page in the new/ folder.
Could it be that you are using absolute links in your pages and not relative ones? For instance if a link looks like "/sample", when in your main site it will link to http://.../sample and the same is true if the link is inside a page under "/new/". If you'd use just "sample" then that would resolve as http://..../sample or http://...../new/sample, depending on the URL of the page.
Having a second htaccess file in a subdirectory shouldn't be an issue, and as far as I can tell, your two look okay.
Are you sure the links in the site are correct? (ex, they are /new/foo, not just /foo)?
I need to redirect if someone goes to any folder e.g. http://site.com/images/ or http://site.com/images to http://site.com.
Unless he goes to file e.g. http://site.com/images/index.php in this case it does not redirect
now i use
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
but i think its not perfect because e.g. if someone comes on http://www.site.com it does not work.
Keep in mind due to server configuration i need to put .htaccess in every folder.
I believe that what you are looking for is the -f flag:
RewriteCond %{REQUEST_FILENAME} !-f
See http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you are only looking at stopping someone from getting a list of the files you have in the folder, you may want to consider instead adding:
Options -Indexes
This will tell apache not to display a directory list if no index page is present in the given directory.
See http://httpd.apache.org/docs/2.2/mod/core.html#options
The last thing to note, if you are not familiar with how .htaccess works, apache scans the current folder and any of its parent folders - you should be okay with only adding a single .htaccess file in your web root for http://site.com. This has been covered in another question here.
I want to have a PHP file catch and manage what's going to happen when users visit:
http://profiles.mywebsite.com/sometext
sometext is varying.
E.g. It can be someuser it can be john, etc. then I want a PHP file to handle requests from that structure.
My main goal is to have that certain PHP file to redirect my site users to their corresponding profiles but their profiles are different from that URL structure. I'm aiming for giving my users a sort of easy-to-remember profile URLs.
Thanks to those who'd answer!
Either in Apache configuration files [VirtualHost or Directory directives], or in .htaccess file put following line:
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,NC,QSA]
</IfModule>
It will silently redirect all incoming requests that do not correspond to valid filename or directory (RewriteCond's in the code above make sure of that), to index.php file. Additionally, as you see, MultiViews option also needs to be disabled for redirection to work - it generally conflicts with these two RewriteCond's I put there.
Inside index.php you can access the REQUEST_URI data via $_SERVER['REQUEST_URI'] variable. You shouldn't pass any URIs via GET, as it may pollute your Query-String data in an undesired way, since [QSA] parameter in our RewriteRule is active.
You should use a rewrite rule..
In apache (.htaccess), something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
Then in your index.php you can read $_GET['url'] in your php code.
You can use a .htaccess file (http://httpd.apache.org/docs/1.3/howto/htaccess.html) to rewrite your url to something like profiles.websites.com/index.php?page=sometext . Then you can do what you want with sometext in index.php.
An obvious way to do this would be via the 404 errorDocument - saves all that messing about with mod_rewrite.
If you have not heard about MVC, its time you hear it, start with CodeIgniter, its simplest and is quite fast, use default controller and you can have URLs like
domain.com/usernam/profiledomain.com/usernam/profile/editdomain.com/usernam/inboxdomain.com/usernam/inbox/read/messageid Or use .htaccess wisely