.htaccess default directory behaviour - php

I have just started learning about .htaccess files for Apache, I have a website set up so that all requests should come through my index file (which is called Main.php).
"Webpages" are then acquired through a wp GET var (such as wp=forum) - i wish to make this instead Domain/directory/Forum instead of the current Doman/directory/Main.php?wp=Forum
The problem i am facing is that all my "webpages" are stored in their own directory and are made up of "webparts" so forum will be a sub directory of "forum" with files inside it that make up the page. This is causing problems with my redirecting.
I have created the following .htacess file:
#turn redirect engine on
RewriteEngine On
#make sure URI is not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^?]*)$ Main.php?wp=$1 [NC,L,QSA]
This works fine on the redirecting until it is a directory that is entered as the "wp" at which time it will add a trailing slash and corrupt the wp GET var passed (forum/ would be passed not forum as needed)
Here are 2 examples of how I think it may be working:
Main (not a file or directory)
loop 1: Hits the rewrite changes to Main.php?wp=Main
loop 2: URI is file (no change)
MainContent (is a directory)
loop 1: Hits the default directory change? (guess) Changes to
MainContent/ (note the trailing slash)
loop 2: Starts .htaccess and changes this to Main.php?wp=MainContent/
loop 3: URI is file (no change)
Moreover, whenever a file is accessed in my websever with a trailing slash after to (so example: Main.php/) it will display with no links or included files. It will just show the file being requested and seems to ignore any and all css ... php includes or anything else (is this default Apache settings?).
Sorry for the long and possible confusing post. If I need to clean anything up just shout.

Try with that:
#turn redirect engine on
RewriteEngine On
DirectorySlash Off
#make sure URI is not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ Main.php?wp=$1 [NC,L,QSA]

It might be helpful to look at the .htaccess file for Drupal, which does the same thing - i.e. redirect all requests through the index file. The most relevant snippet from that file is:
# Pass all requests not referring directly to files in the filesystem to index.php?page=[rest of url]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
The second condition above "! -d" might solve the problem you describe but you might still need to do some "cleaning" in your php code. It's worth looking at the whole Drupal .htaccess file as it does other things to tighten security, such as blocking access to certain files etc.

Related

Changing URL so post/{number} in HTML resolves post?id={number}, but still displays post/{number} as URL

I'm struggling pretty hard with .htaccess. Even though I looked up quite a few solutions here, I couldn't get to setup the following:
I have a file called index.php. Inside the index.php I have a link like
Post
Clicking the link, should lead to a file in the same directory called post.php. Inside the post.php I'm grabbing the id through $GET['id'].
But I still like to display post/12345678901 as the URL.
I already tried editing the .htaccess but clicking the links leads to a 404.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^post/([^/]+) /post.php?id=$1
You have a RewriteRule but you probably need a ReWriteCondition to go with it else this rewrite will be applied to every call to a page on the apache.
Options +FollowSymlinks
RewriteEngine on
# Not a file
RewriteCond %{REQUEST_FILENAME} !-f
# not a directory
RewriteCond %{REQUEST_FILENAME} !-d
# Edited to show root location of files.
# also added NoCaseSensitivity flag.
RewriteRule ^post/([0-9]+)/? /post.php?id=$1 [NC]
Also remove the first / in the second part of the RewriteRule because that will be domain.com/post.php which might not be your actual file location.
Your HTML appears to be showing relative filepathing which is not a good idea and might come back to bite you in the future, As a recommendation every URL on the same domain on your HTML should start with a / so;
Post
Which can then be manipulated by the .htaccess to go where you need, even in another folder (not the base).

How Do I Make A Common PHP File To A Directory With .htaccess?

I'm looking to handle the URL's except homepage with a common PHP file. This is just like a PHP $_GET request except the difference that there would be no parameter. It'll be just like a file.
Ex- http://localhost/ - This should be managed by index.php file as usual.
http://localhost/ANYTHINGHERE - This should be thrown to a custom PHP file which would then decide what to do.
Actually, I'm working on a project where I need to hide the URL information from the users. So, the file that would manage the ANYTHINGHERE URL would actually access a directory localhost/i/.
Thanks and waiting for best response!
To achieve this you need two parts:
First: .htaccess which redirects all accesses to your domain passed to a php script (index.php here):
RewriteEngine On
RewriteRule (.*) index.php/$1
Second: In index.php you get the user-entered URI as $_SERVER['PATH_INFO'] (starting with /)
This, however, makes all requests to go through the index.php script (depending on the location of index.php you could also get an endless recursion, so read on ;) ). Normally one doesn't want that (e.g., images should be served directly by the web server). Thus, one normally uses (i.e., existing directories, files and links are served by the web server directly):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^(.*)$ - [NC,L]
RewriteRule (.*) index.php/$1
If this should take place in a subdirectory you need to add RewriteBase /subdirectory directly after RewriteEngine On.
If you don't want to use $_SERVER['PATH_INFO']you can also use RewriteRule (.*) index.php?url=$1 [QSA], then you get the user entered URI as $_GET['url'].
This requires mod_rewrite to be loaded on the server.
See http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html.

Rewrite everything after a certain directory to that directories index.php (htaccess)

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.

Redirect rules for 404 content using .htaccess

It's quite common for us to put in progress sites at www.domain.com/dev/ and then once the client has signed off the site to move it to the top level www.domain.com, what we like is to be able to put a .htaccess file in the top level so that once we've moved the site out of /dev if the client accidently goes to www.domain.com/dev/apage.php that they be redirected to www.domain.com/apage.php, but only if www.domain.com/dev/apage.php doesn't exist.
Sometimes the dev folder will be called various other things, and ideally we don't want to have to edit the .htaccess file to match the folder name.
Thanks for any help.
You could do something like
Edited based on comments:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule dev/(.*)$ $1 [R=301,L]
Which basically means that if the file doesn't exist - RewriteCond %{REQUEST_FILENAME} !-f - then rewrite any request to dev back to the root. You have to specify dev/ in the rewrite rule as otherwise you will get stuck in a redirect loop.
This will only work however if you are using explicit files rather than a framework with everything routed through index.php for example

htaccess rewrite ".../pages/about.php" to ".../about"

I've searched and found a lot of questions on this site and elsewhere that are very similar, but I've tried implementing and modifying all the suggestions I've found and none of it works. I realize this is a very basic question an I am extremely frustrated because nothing I'm trying is working.
With that having been said... I am trying to organize my content pages within kurtiskronk.com/pages/... (e.g. kurtiskronk.com/pages/about.php)
What I want to do is make it so that I can simply link to kurtiskronk.com/about ... So how do I go about stripping "pages/" and ".php"? I don't have a ton of content pages, so it's not a big deal if I have to specify for each page, though something dynamic would be handy.
NOTES: I am using Rackspace Cloud hosting, and WordPress is installed in /blog. My phpinfo() can be seen at http://kurtiskronk.com/pages/phpinfo.php
This is my existing .htaccess file (in the root)
php_value register_globals "on"
Options +FollowSymLinks
RewriteEngine On
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
RewriteBase /
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [L]
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
# PHP - MAIL
php_value mail.force_extra_parameters -kurtis#kurtiskronk.com
I tested and the rewrite works with the line below (/about as URL brings up file /pages/about.php), but then the homepage gives a 500 Internal Server Error:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
So I'm still sort of in the same boat as before, and as a follow-up, possibly more difficult question, if you go to http://kurtiskronk.com/weddings I am using SlideShowPro (flash) w/ SSP Director (self-hosted) as the back-end for it. When it pulls up a new image, it adds the following after /weddings ... "#id=album-152&num=content-9698"
There are four sections of the portfolio
# Homepage (kurtiskronk.com) id=album-148 ($id is constant for this section)
# Weddings (/weddings) id=album-152 ($id is constant for this section)
# Portraits (/portraits) id=album-151 ($id is constant for this section)
# Commercial (/commercial) id=album-150 ($id is constant for this section)
Assuming we get kurtiskronk.com/weddings to rewrite successfully without breaking anything, how would we make the total URL something cleaner kurtiskronk.com/weddings/9698 since the $num is the only thing that will change within a given section?
Kurtis, thanks for the extra information. It's a lot easier to give a specific answer to this.
My first comment is that you need to separate out in your thinking URI space -- that is what URIs you want your users to type into their browser -- and filesystem space -- what physical files you want to map to. Some of your mappings are URI->URI and some are URI->FS
For example you want to issue a permanent redirect of www.kurtiskronk.com/* to kurtiskronk.com/*. Assuming that you only server the base and www subdomains from this tree, then this cond/rule pair should come first, so that you can assume that all other rules only refer to kurtiskronk.com.
Next, you need to review the RewiteBase documentation. .htaccess files are processed in what Apache calls a Per-Directory context and this directive tells the rewrite engine what to assume as the URI base which got to this directory and .htaccess file. From what I gather, your blog is installed in docroot/blog (in the filesystem, and that you want to get to directory by typing in http://kurtiskronk.com/blog/ but that this .htaccess file is for the root folder -- that is the base should be (this goes before the www mapping rule)
DirectorySlash On
DirectoryIndex index.php
RewriteBase /
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
You can add some field dumps look for REDIRECT_* in the Server or Environment table in the phpinfo O/P to see if these are sensible. For example:
RewriteWrite ^(.*)$ - \
[E=TESTDR:%{DOCUMENT_ROOT}/pages/$1.php,E=TESTPDR:%{DOCUMENT_ROOT}/pages/$1.php]
Your next rule is that if the file exists in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [NS,L]
[Note that some shared service sites don't set up DOCUMENT_ROOT properly for the rewrite engine so you may need to run a variableinfo script (<?php phpinfo(INFO_ENVIRONMENT | INFO_VARIABLES); to see if it sets up alternatives. On your site you have to use %{ENV:PHP_DOCUMENT_ROOT} instead.]
Your next rule is that if the file exists, but with the extension .php in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [NS,L]
Now redirect any blog references to the blog subdirectory unless the URI maps to a real file (e.g. the blog stylesheets and your uploads.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
A complication here is that WP may be using a poorly documented Apache feature call Path Info that is a script can act as a pseudo directory so http://kurtiskronk.com/blog/tag/downtown/ is redirected to docroot/blog/index.php/tag/downtown/ which is then executed by `docroot/blog/index.php using /tag/downtown/ as the PATH_INFO. But this is one for Wordpress experts to comment on. If this last rule doesn't work then try:
RewriteRule ^blog/(.*) blog/index.php/$1 [L]
PS. I like your site. I wish I was that young again :(
Postscript
When you say "it doesn't work", what doesn't with this .htaccess?
http://kurtiskronk.com/phpinfo,
http://kurtiskronk.com/phpinfo.php,
http://kurtiskronk.comblog/tag/downtown/
It's just that these rules work for these tests (with domain swapped) on mine. (One way is to move or copy the above variableinfo.php to the various subdirectories. If necessary temporarily rename the index.php to index.php.keep, say, and copy the variableinfo.php to the index.php file. You can now enter the various URI test patterns and see what is happening. Look for the REDIRECT_* fields in the phpinfo output, and the SCRIPT_NAME will tell you which is being executed. You can add more {E=...] flags to examine the various pattern results. (Remember that these only get assigned if the rule is a match.
Lastly note the changes above especially the additional NS flags. For some reason mod_rewrite was going directly into a subquery which was resulting in redirect: being dumped into the file pattern. I've had a look at the Apache code and this is a internal botch to flag that further redirection needs to take place (which then replaces this or backs out). However this open bug indicates that this backout can be missed in sub-queries and maybe that's what is happening here. Certainly adding the NS flas cured the problem on my test environment.
PS. Note the added explicit DirectoryIndex directive and also that whilst http://kurtiskronk.com will run the root index.php, the explicit /index.php version will run the one in pages, because that's what your rules say.
Here is a simple solution. You can use it apache conf file(s) or in .htaccess (easier to set up when you're trying).
mod_rewrite has to be enabled.
For example, use .htaccess in your DocumentRoot with:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
It will redirect /about to /pages/about.php, and any other page.
The "RewriteCond" part is to authorize access to an existing file (eg: if you had an "about" file at the root of your site, then it will be served, instead of redirecting to /pages/about.php).
Options +FollowSymLinks
RewriteEngine On
RewriteRule /([0-9]+)$ /pages/$1.php [L]
Put something like this in your .htaccess file. I guess that is what you want.
Juest a redirect from a simple url to a longer url.

Categories