Redirect all .php traffic to .php5 files - .htaccess

I have a personal website with a MediaWiki installation on a shared host. The Apache configuration treats all .php request with PHP 4, and all .php5 requests with PHP5.
For compatibility reasons I need to be able to use the .php extension, but MediaWiki is only available on PHP5. I tried to use the mod_rewrite engine, but I'm stuck with the rule.
Here's the current file:
DirectoryIndex index.php5
RewriteEngine on
# This rewrites URIs in the form /pages/Article_Name to /index.php5?title=Article_Name.
RewriteCond %{REQUEST_URI} !^/pages/index.php5.*$ [NC]
RewriteRule ^pages/?(.*)$ /index.php5?title=$1 [L]
# This is the broken rule
RewriteCond %{REQUEST_URI} ^index\.php(([^5])(.*))?$ [NC]
RewriteRule ^index\.php(([^5])(.*))?$ /index.php5?$1 [L]
The idea of the rule was "Redirect all content from index.php (not followed by '5') to index.php5".
Any idea?
Edit:
SetEnv PHP_VER 5
works, but I'm still interested on why the rule was not taken into account.

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.php $1.php5 [R=301,L]
RewriteRule ^(.*)\.php $1.php5 [L]
I found this on the following web site which may be useful, you could make it more specific and remove the catchall to change only a certain number of .php files if you liked.
You can also customize it to suit only a certain directory using the RewriteBase condition.

I would change hosts, you shouldn't have to have php applications state their version after the extension. It was ghetto with PHP3 and very ghetto and bad practice for php5.
While of course its possible this is bad practice and you will most likely never see an OSS application built around a php5 file extension naming convention.
I would quit while you're ahead and jump ship on a bad host - not try and alter how an application like mediawiki is built to operate. The hosts I've seen that have both php4 and php5 allow you to choose which install you would like to run for the domain - not designate it with a file extension. Thats ghetto.

Try this
AddType x-mapp-php .php5
It might be the other way around, try it though, i cant atm :S

The REQUEST_URI value always starts with a slash but your pattern doesn’t.

This seems to work for me:
AddType application/x-httpd-php5 .php

Related

how to rewrite SEO URL

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/files/section.php?id=1
Changed to:
demo.example.com/sample-section
I tried
Since you use .htaccess I assume you are using Apache. Here you'll find all relevant documentation.
First of all you need the mod_rewrite module to be installed (instructions to do so depend on the server's operating system and Apache distribution).
Then, the URL rewrite is pretty simple:
# First of all tell to mod_rewrite to operate.
RewriteEngine on
# Then, as many times you need, tell it on what to operate...
# For example: on files that do not exist. Or leave out RewriteCond to act on all.
RewriteCond "%{REQUEST_FILENAME}" !-f
# ...and what to do.
RewriteRule /sample-section "/files/section.php?id=1" [PT]
RewriteRule /another-section "/files/section.php?id=2" [PT]
The PT (PassThru) flag might be needed in some contexts, otherwise just use [L].

Why deosn't this rewriterule work?

The company I currently work for has a 3+ years live site based on symfony 2.8. Recently it was deployed for testing to a new customer. The images on the original are accessed in the html like "/bundles/bundlename/images/image.png".
On the customers server however the whole thing is under a subfolder, so image url's should be like "/d2s/bundles/bundlename/images/image.png".
We have a .htaccess file in the project's web dir like this:
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
FollowSymLinks On
RewriteRule /?bundles/(.*)$ /d2s/bundles/$1 [NC,R=301,L]
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the app.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
[,,,]
All other parts work fine, in apache conf's virtualhost AllowOverride is set to All. But this url rewrite just doesn't work, like if it wasn't there at all.
Any idea what can be wrong?
Set a correct RewriteBase. Don't start your internal RewriteRule patterns or substitutions with a /. If you're going to stick with with the auto determination of a RewriteBase equivalent, it should precede all other rules.

.htaccess cans and can'ts

I am very new to the idea of .htaccess and thought that it was what you used to do something like turn this:
http://www.domain.com/some/ugly/url/here.html
into this:
http://www.domain.com/niceurl
I was just told by my ISP that in order to get that to happen, no, it's done by putting the document into the web root folder. That .htaccess isn't used at all.
Does anyone know if this is true? I see a lot of examples about what .htaccess DOES but not so much about what it can't do. Somehow I thought this was all that was needed.
Lastly, if someone types in www.domain.com/niceurl what will happen? Don't I need to have that linked (if not by htaccess, how?!) to the location of the actual file?
Thank you for any and all help. I realize that .htaccess questions abound but they're hard to pick through for the layperson and I'm hoping to answer this specific question.
Here's what I believe should be an answer you want, put the block below to your .htaccess
Answer:
## Enabling Apache's Mod_rewrite module.
RewriteEngine On
# Following line is required if your webserver's URL is not directly related to physical file paths (just / for root, e.g. www.domain.com/)
RewriteBase /
# Restricts rewriting URLs only to paths that do not actually exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Redirect www.domain.com/bar to www.domain.com/foo
Redirect 301 /bar /foo
# Internally load the long URL without changing URL in address bar
RewriteRule ^foo/?$ http://www.domain.com/some/ugly/long/thing/here.html [L,NC]
As a result, www.domain.com/bar will be redirected to www.domain.com/foo and /foo will internally load http://www.domain.com/some/ugly/long/thing/here.html
FYI:
Your website's URL doesn't have to be directly related to physical file paths. Your URL's segment can be served as alias to your URL's parameters. for e.g,
http://www.domain.com/index.php?key1=value1&key2=value2
can be represented as
http://www.domain.com/value1/value2
Note: you need to implement a server side script to be served as a
router to manipulate the URL segments.
For more information about using .htaccess, check this out
Ref: http://htaccess-guide.com/
.htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.
Below is a few examples,
# Custom Error Pages for Better SEO,
# for e.g, to handle 404 file not found error
ErrorDocument 404 http://www.domain.com/404page.html
# Deny visitors by IP address
order allow,deny
deny from 122.248.102.86
deny from 188.40.112.210
allow from all
# Redirects
Redirect 302 /en/my-dir/my-page.html /en/my-path/example.html
# Disallow some silly bots from crawling your sites
RewriteCond %{HTTP_USER_AGENT} (?i)^.*(BlackWidow|Bot\\ mailto:craftbot#yahoo.com|ChinaClaw|Custo|DISCo|Download\\ Demon|eCatch|EirGrabber|EmailSiphon|EmailWolf|Express\\ WebPictures|ExtractorPro|EyeNetIE|FlashGet|GetRight|GetWeb!|Go!Zilla|Go-Ahead-Got-It|GrabNet).*$
RewriteRule .* - [R=403,L]
# Setting server timezone
SetEnv TZ America/Los_Angeles
# trailing slash enforcement,
# e.g, http://www.domain.com/niceurl to http://www.domain.com/niceurl/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !#
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.domain.com/$1/ [L,R=301]
Enable mod_rewrite and .htaccess through httpd.conf (if not already enabled) and then You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^niceurl/?$ some/ugly/url/here.html [L,NC]
This will allow you to use http://domain.com/niceurl in your browser and it will internally load http://domain.com/some/ugly/url/here.html without changing URL in browser.
If you also want to force redirection from ugly URL to pretty URL then add this redirect rule just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} \s/+some/ugly/url/here\.html [NC]
RewriteRule ^ /niceurl [R=302,L,NE]

Make Folders in Apache Case Insensitive using .htaccess

I need to make accessing directories on my server case insensitive.
How do I do that using htaccess?
You have to install and enable the mod_speling module in apache and set the CheckCaseOnly Directive to On in your .htaccess
CheckCaseOnly On
If you want requested URLs to be valid whether uppercase or lowercase letters are used, use mod_speling to make URLs case-insensitive. Write the following code in .htaccess file:
CheckSpelling On
This is what I used because my hosting is shared and does not include the mod_spelling module but does support .htaccess, but this only works for one folder:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^student-government/$ http://www.tombarrasso.com/Student-Government/ [R=302,NC,L]
The folder to redirect to can be any case, so you could use lower-case folders and redirect all variations of spelling there.
I suppose it could be adapted with a little bit of REGEX to work for all folders rather than just one. This worked for me on Apache 2.2.14 (Unix).
Solution-1: To make case-insensitive directory and files names with respect to the requested URL we can add the following two lines to the app's .htaccess file:
<IfModule mod_speling.c>
#Once enabled, mod_speling redirects misspelled requests to any nearest matching resources. Uses a bit of memory, but can be useful if you've been changing URIs or have lots of similarly named URIs:
CheckSpelling On
CheckCaseOnly on
</IfModule>
Solution-2:
Again if we want just a few predefined directories to go to specific directories, then apply the below lines instead:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^my-dir/old-dir-1/$ /my-dir/new-dir-1/ [R=301,L,NE]
RewriteRule ^my-dir/old-dir-2/$ /my-dir/new-dir-2/ [R=301,L,NE]
RewriteRule ^my-dir/old-dir-3/$ /my-dir/new-dir-3/ [R=301,L,NE]
# For any case insensitive directories we can try adding NO-CASE (NC)
RewriteRule ^My-Old-Dir/$ /my-new-dir/ [R=301,L,NC]
</IfModule>

CakePHP and .htaccess in shared hosting environment

Greetings!
I have CakePHP based app on shared hosting I wonder if there's a way to clean up the url through .htaccess. What bugs me is that I have to have index.php in it or I get a 404:
project.com/index.php/controller/method
Initially I was getting a 404 error no matter what and my host admin ended up setting RewriteEngine off and this is what it looks like now
<IfModule mod_rewrite.c>
RewriteEngine off
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Is there fix for this without the .htaccess? As it is right now, does it pose any type of security risk?
Thanks
You need three .htaccess files :
/.htaccess
/app/.htaccess
/app/webroot/.htaccess
If the one you pasted in your question is the one at the root of your website, that's probably where your problem comes from. These directives file would rewrite URLs to project.com/webroot/, which doesn't exist. It should redirect to project.com/app/webroot/, which will in turn rewrite to index.php?url=$1 (relative to project.com/app/webroot/).
I'm not pasting the files here; the three of them are available in the CakePHP releases as well as in the Book: http://book.cakephp.org/2.0/en/installation/url-rewriting.html (check the 3rd item in the page).
Are you sure the mod_rewrite module is enabled on your shared hosting?

Resources