In the httpd.conf file I have AllowOverride FileInfo. In the .htaccess file in top level of my webserver with all the other files, I have this:
RewriteEngine On
RewriteRule ^downloads/?$ index.php?page=downloads [L,NC]
But it doesn't work. mywebsite/downloads and mywebsite/downloads/ always give a 404 not found. Any idea why? Thanks. (mywebsite/index.php?page=downloads does work).
And I'm restarting apache every time I change it.
And when I put the code above in httpd.conf, the website won't even load at all, just blank, spinning safari wheel forever.
Its fine if I just do RewriteEngine On, but if I do anything else (RewriteBase, RewriteRule), the web browser spend ages trying to load and finally giving this error:
Safari can’t open the page “http://mk12.gotdns.com/” because the server where this page is located isn’t responding.
Anyone have any idea what's wrong?
EDIT: I'm able to make, for example, css files forbidden with rewrite, and it works, but any rule that goes from downloads to index.php?page=downloads makes the server not respond (see above), it doesn't matter what page, the website won't load at all. Any ideas..?
The best way to debug rewrite rules is to enable rewrite logging so you can see what's going wrong.
It Worked!! I was putting the code in the wrong spot. It was in httpd.conf, but at the end. Move it into <Directory> and its good. Thanks for your help!
EDIT: Also, I found that It won't work if the flags are like this: [L, NC]. It has to be [L,NC] (no spaces).
So the two problems where that it wasn't inside <Directory "/Library/WebServer/Documents">, and there were spaces between the flags. Hopefully this will help someone else in the future.
Have you tired adding a slash in front of the "download" like below
RewriteRule ^/downloads/?$ index.php?page=downloads
EDIT: Try the code below:
RewriteEngine On
RewriteBase /
RewriteRule ^downloads/?$ /index.php?page=downloads
I would try removing the trailing slash and question mark after downloads, and the leading slash before index.php.
RewriteEngine On
RewriteBase /
RewriteRule ^downloads$ index.php?page=downloads.
Related
Struggling. Help!
In index.php:
require "cachedPages/home.html";
If I visit: https://websiteaddress.org/index.php then it works fine.
If I visit: https://websiteaddress.org then I get an internal server error.
I guess it's a .htaccess thing. All I have in there is some cpanel php72 code and:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DirectoryIndex index.php
It seems there's some difference between how index.php is called if you call it directly as opposed to .htaccess calling it for you?!?
Any ideas?
Thanks in advance.
If you don't have one of the following options in your .htaccess then Apache won't know which file to default back to:
DirectoryIndex index.php
FallBackResource /index.php
Also I'd recommend installing mod_rewrite as well. It's handy for other reasons.
Well, I've managed to sort the problem, but it's a pretty weird situation that no-one else will probably ever experience. Here goes...
If the user lands at:
https://websiteaddress.org rather than https://websiteaddress.org/index.php
and that page (php) requires another page,
which has images that are embedded as URIs rather than linked src files.
Then, the first URI causes a server error.
If I replace the
img src='data:image/jpeg;base64,/998a9g98ahg...etc'
with
img src='path/to/file.jpg'
on the first instance of a jpg then it all works fine.
All the later URIs are fine, it's just the first instance!
It all works now, with this workaround; and the situation is so unique and bizarre that I doubt this thread will be of use to anyone else. In fact it's so edge-case that I can't be bothered investigating it any further myself.
Alright, so I'm really struggling right now, I've tried everything, read articles and tutorials but I still don't know why this doesn't work.
I've got an index.php file:
echo "some test";
if(isset($para)) {
include $para . '.php';
} else {
echo "there's no parameter";
}
So if the user views the page with the parameter 'home' (http://mypage.com/test/index.php?para=home), he'll get the correct page. That does work.
.htaccess:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mypage.com$ [NC]
RewriteRule ^(.*)$ http://mypage.com/test/$1 [R=301,L]
RewriteBase /test/
RewriteRule ^([a-z]+)$ index.php?para=$1
But when I try to navigate using the rewritten link (http://mypage.com/test/home), the browser only outputs the included file and it completely ignores the index.php and its content.
Is there something wrong with my htaccess file? Or do I need to change some server configuration? Because I had a different htaccess file on a different webserver and it worked without any issues. But it didn't work on the webserver I'm using right now (sadly, I only have access to the htdocs folder).
EDIT: Just checked phpinfo, the page is running on PHP 5.3.28 on apache2.
Solved!
Problem solved. Adding 'Options -Multiviews' fixed the issue.
The hint here is:
The browser only outputs the included file and it completely ignores
the index.php and its content.
Try disabling mod_negotiation (Options -Multiviews) in the context where you want to do this remapping with mod_rewrite and within your script.
mod_negotiation is the most common reason "other" rewriting seems to be happening on your resources.
I am trying to rewrite a directory to its own sub folder. I have an .htaccess file in the directory, and I also put the AllowOverride All in the Apache conf file.
Basically, I want the server to redirect this url: http://example.com/MPOS to http://example.com/MPOS/public. The closest I got was using this:
RewriteRule /MPOS$ /MPOS/public/ [L]
It does the job, going to http://example.com/MPOS/public, but its the rest of the resources (all the pages, stylesheets and the rest) are not being redirected, and so I get the page as if the links are broken. Inside public folder there is "index.php" file.
I know there are many questions about it, but after hours of search nothing helped me, so I posted this question.
Any help would be really appreciated.
Thanks,
Shay
OK, I feel a bit stupid. Managed to get it to work by writing the rewrite like this:
RewriteRule ^$ /MPOS/public/ [L,R]
When the .htaccess is found in the "MPOS" folder.
I am trying to redirect www.example.com to /example/ on my webserver, but it appears to only be redirecting the index.php page. An additional issue is the main file displays as http://tunedu.com/tunedu/ when I would like it to display as tunedu.com
Live example:
This page works: http://tunedu.com/tunedu/
This page doesn't work: http://tunedu.com/school.php?id=75
Any regex changes I do try end up just breaking everything. The .htacess code is this:
RewriteEngine on
RewriteRule ^$ /tunedu/ [L]
Thanks.
The proper way to do such a thing is by setting VirtualHosts on your webserver (either Apache, nginx or another...). Using htaccess for this seems quite painful.
Assuming you're using Apache, here's a useful link: http://httpd.apache.org/docs/current/vhosts/examples.html
I'm not sure why you are not using VirtualHost to set that up.
But in case you want to go the mod_rewrite way here is an useful link:
http://httpd.apache.org/docs/2.2/rewrite/vhosts.html
I hope that helps.
I've created a .htaccess file, and can get the server to serve me a custom 404 page. However, when i try and do a re-write, it doesn't work.
The htaccess file is located in the root directory.
RewriteEngine On
RewriteRule /htaccessTest/index.php /htaccessTest/phpinfo.php
ErrorDocument 404 /htaccessTest/errors/404_message.php
I'm not sure as to why this isn't working. I've checked phpinfo, and under Apache2, it states that mod_rewrite has been loaded.
I'm really stumped here. Not sure what's going on!
Any help would be great!
Update 1
As per what was highlighted below, i found that it still didn't work! I've made some changes, and now all I get is the 404 error message, unless I navigate to the index page.
Here are the changes:
ErrorDocument 404 /htaccessTest/errors/404_message.php
RewriteEngine On
RewriteRule ^htaccessTest/test\.php$ /htaccessTest/phpinfo\.php$ [L,NC]
And i access it using the url:
localhost/htaccessTest/test.php
(ignore the lack of http://, I've removed so that it didn't think it was a link).
Any further ideas?
Thanks
Try your rule without leading slash:
ErrorDocument 404 /htaccessTest/errors/404_message.php
RewriteEngine On
RewriteRule ^htaccessTest/index\.php$ /htaccessTest/phpinfo.php [L,NC]
Better to use line start/end anchors to avoid matching unexpected text in an URI.
dot needs to be escaped in a regex which otherwise matches any character.
.htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
Finally found the answer after hacking around with it.
It seems to be the first part of the Rewrite.
There is a difference with the first and second section that I didn't realise, which is that the URL sort of "resets" when using Rewrite.
This is explained easier by giving you the now re-worked example.
Original:
RewriteRule ^htaccessTest/test\.php$ /htaccessTest/phpinfo\.php$ [L,NC]
New:
RewriteRule ^test.php /htaccessTest/phpinfo.php
As you can see, the first part references htaccessTest. You don't need this part, as the url was "already in" this file structure. Your url being: "localhost/htaccessTest/test.php"
The second part stays roughly the same, I just removed the Regex stuff and the [L,NC]. The [L, NC] will probably come in handy later, but for simplicity sake I removed it.
The result is this:
ErrorDocument 404 /htaccessTest/errors/404_message.php
RewriteEngine On
RewriteRule ^test.php /htaccessTest/phpinfo.php
Thanks for the help guys! (Especially #anubhava)