Can't redirect with .htaccess - php

On the end of my rope here, nothing that should be working is.
I'm new to website building and I noticed my visitors were trying to access certain pages and getting forbidden warnings e.g.: trying www.test.com/test you give a 403 error.
All I want to do is make it so www.test.com/test would redirect it to a test.html file in a directory right above it.
Tried doing it with .htaccess in the root directory with nothing but
redirect /test www.test.com/test.html
Doesn't work. Just gives me the 403 error.
Tried a simple index.html file to redirect with nothing but
<?php
header('Location: www.test.com/test.html');
die();
?>
But that doesn't work either.
Really not sure why both of these outright refuse to work.

header parameter should be wrapped in double quotes. like
header("Location: www.test.com/test.html");
exit();
also make sure your server recognizes your file extention as a php file if its not having a .php file extension. You can add the custom extensions by editing httpd.conf.

If I am interpreting correctly your ultimate intention, you don't need to use redirections, just try this in your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html [QSA,L]
</IfModule>
This way, every requested URL will be interpreted as if it was the same request plus .html.
Although you could do it via a redirection, it is not necessary.
So once you have that code in your .htaccess you just need to have the file test.html in your root, and www.test.com/test will serve the www.test.com/test.html page.

Related

Treat an incomplete filename in a URL as PHP?

I didn't write this web code. I just have to deploy it....
The $WEBROOT directory includes index.php and login.php, among others.
The automatic redirect URL from index.php (which successfully executes when just the domain URL is requested) is http://$HOST.$DOMAIN/login?p=$VALUE
This returns the code 404.
If I manually change the URL to http://$HOST.$DOMAIN/login.php?p=$VALUE
the login page successfully appears.
My first problem is I don't know what keywords to search for in the Apache documentation for this. This question seems close. But, my actual files have the .php extension. My problem is that I have to assume all of the URLs will just request file and not file.php.
How do I tell Apache to look for file.php before it returns a 404 for not finding file?
Create a file named .htaccess with the following content inside the folder your .php files are in:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Showing php as html by '.htaccess', also php extension will not work

I really need help regarding this. I know that if I add the following line in .htaccess file, php will show as html.
RewriteRule ^(.*)\.html$ $1.php
When I write http://example.org/abc.html, actually abc.php is executing. But when I write http://example.org/abc.php, it also works.
Is there anything I can add in the .htaccess file where .php will not work. I mean when I will write abc.html, abc.php will execute. But if I write abc.php, it will show 404 error.
Try adding this to your .htaccess:
RedirectMatch 404 \.php$
RedirectMatch does not match the query string, so this should even redirect whatever.php?someParam=someValue.
Adding this rule should redirect .php files to a 404
RewriteRule ^(.*).php - [R=404]
The R=404 part sets the return code to a 404. Here's documentation on the R option: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_r. And the "-" means that the URL is passed through untouched.
You'll also need to add this before either of your two redirect rules (taken from http://www.askapache.com/htaccess/modrewrite-tips-tricks.html#Fix_infinite_loops):
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
This will prevent a redirect loop from happening that will present the user a 404 error when they use the .html extension as well as the .php extension.

Redirecting a download file to the new named file via htaccess

Over time, filenames of our software have changed for various reasons, so an old download file pointed to by shareware sites no longer exists.
I want to redirect so that if somebody clicks a link to the old file they can download the new (different named) version.
So I tried adding a .htaccess file into public_html that contains the following (and nothing else):
Redirect 301 mydomain.com/oldinstaller.exe mydomain.com/newinstaller.exe
But that didnt work, I get 500 errors issued by the server. All files are in public_html and so is the .htaccess file. Any ideas what I am doing wrong?
UPDATE
I tried the following:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^oldinstaller.msi /newinstaller.exe [R=301,L]
and the browser is given this URL:
https://example.com/home/admin/web/example.com/public_html/newinstaller.exe
And if I try:
RewriteRule ^oldinstaller.msi$ example.com/newinstaller.exe [R=301,L]
I get:
https://example.com/example.com/newinstaller.exe
Try mod_rewrite redirecting using
RewriteRule ^oldinstaller.exe /newinstaller.exe [R=301,L]
or redirect by adding HTTP headers in your php page
This should make a trick:
Filename: .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^oldinstaller.exe$ mydomain.com/newinstaller.exe [R=301,L]

Redirecting all request to the root directory somewhere else?

Alright, so I've downloaded a CMS for a server I'm setting up and I have some difficulty with the path the files are on.
I have put the CMS in a subdirectory on my server as the root directory is already being used by another CMS. However, the CMS repeatedly uses "/" in the code to link to their files. For example, an image is found using this code:
<img src="/images/banner.png" />
As you know this will not work, because the link above redirects the request to the images folder in the root of the server, not the subdirectory. The CMS also came with a ".htaccess" file, so I immediately thought that I could redirect all requests made using the "/" character to the subdirectory on the server. This is the original ".htaccess" file:
RewriteEngine On
RewriteRule ^index.php(|/)$ content/.php
RewriteRule ^error.php(|/)$ content/error.php
RewriteRule ^housekeeping(|/)$ housekeeping/index.php
RewriteRule ^(|/)$ content/$1.php
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ content/$1.php
RewriteRule ^rd/([^/]+)(|/)$ /rd.php?id=$1
RewriteRule ^quickregister/start(|/)$ /register/start.php
RewriteRule ^quickregister/step2(|/)$ /register/step2.php
RewriteRule ^quickregister/step3(|/)$ /register/complete.php
RewriteRule ^community/online(|/)$ content/online.php
RewriteRule ^community/vip(|/)$ content/vip.php
RewriteRule ^credits/goldvip(|/)$ content/goldvip.php
RewriteRule ^home/(..*)$ content/home.php?u=$1
RewriteRule ^articles/(..*)$ content/articles.php?story=$1
ErrorDocument 403 /content/error.php
ErrorDocument 404 /content/error.php
I thought that by adding
RewriteRule ^/$ /subdirectory/
the requests to "/" would be redirected, but to no avail. I have checked the apache configuration and it seems that overwriting the config using htaccess files is enabled, so if anyone is able to help me out I'd be very happy.
Thanks in advance!
EDIT:
I came real close to a solution. I inserted this code just below "RewriteEngine on":
RewriteRule ^(.*)/(.*)$ /private_servers/strebbohotel/$2
This returned the following error page when visiting the url "http://mysite.com/private_servers/strebbohotel/content/.php":
Not Found
The requested URL /private_servers/strebbohotel/.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
As you can see, it skips the "content" subdirectory for some reason. I believe it has something to do with the other lines of the .htaccess file but I can't figure out which ones. It also could be that I need a more complex regex, but I'm not particularly good with it so if someone could help me further I'd be thankful.
You have to change the .htaccess file on yout root directory - and that will mess with the other CMS. The only solution that comes to my mind is to use combination of RewriteCond's:
RewriteCond %{HTTP_REFERER} my_better_cms_url_regex
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^.*$ /subdirectory/%{REQUEST_URI}
That should do the trick. If you want to customize it, refer to this cheatsheet (or simmilar). But be aware, that .htaccess in root directory is checked against ALL requests made to your subdir cms - therefore the need for second condition.
The .htaccess file in your CMS subdirectory would have no effect on requests made against the server root directory. You would need to add an .htaccess there.
Even then though, I don't know how you would differentiate requests from the CMS (like the image request in your example) from those intended for the application in the web root.
Does your CMS not have any configuration setting that allow you to specify a path the the files other than the web root?
You want to use:
RewriteBase /another_root_dir/
in the beginning of your .htaccess file

TinyMCE / Tinybrowser upload tab giving a 404

Just looking for a bit of help with Tinybrowser - I've tried updating it to the latest version but it's still failing. If I click the 'Upload' tab I get a 404.. I'm thinking it's possibly to do with the htaccess, but I'm not too hot with htaccess rules so I can't pinpoint it.
If I visit the page directly (/admin/javascript/tiny_mce/plugins/tinybrowser/upload.php?type=image&tokenget=315af6ee7cf85bc6170760a0c1a5b86d&folder=) I get a 404 as well. If I take off 'folder=' it'll go to the page, if I make it follllder=, it'll go to the page. So for some reason 'folder=' seems to be causing problems. Even if I manually put in a string for the folder var.
The htaccess is as follows:
# default
Options All -Indexes
RewriteEngine On
RewriteBase /
# Make sure there's no way of getting to this file
<FilesMatch "\.htaccess$">
Order deny,allow
Deny from all
Satisfy all
</FilesMatch>
RewriteRule ^login(/*)$ system/login/ [L,QSA]
# Route all other traffic to index.php (front controller)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?_args=$1 [L,QSA]
AddType text/x-component htc
If I remove
RewriteRule ^(.*)$ /index.php?_args=$1 [L,QSA]
I end up getting a 403 forbidden error, but obviously it's not really feasible to remove this line anyway.
Thanks in advance guys
I just ran into this as well.
The problem appears to be a security restriction on some servers that relates to the filename of the PHP file, as well as the "folder=" querystring.
I got around it by renaming upload.php to upload_tab.php, and then modifying tinybrowser.php to reference the upload_tab.php file:
if($tinybrowser['allowupload'])
{
?><li id="upload_tab"><span><?php echo TB_UPLOAD; ?></span></li><?php
}
This allows you to see the upload window, and submit some files using POST variables.
However, a problem remains...
The particular server I came across also has a similar restriction on a request to upload_file.php with a querystring of 'folder='. There isn't an easy fix for this, apart from decompiling flexupload.swf (which I tried, but couldn't recompile it) so I couldn't fully resolve the issue.

Categories