I am trying to perform this type of rewrite
http://sitename/foo/var1/var2 -> http://sitename/foo/index.php?/var1/var2
This is my .htaccess file(placed in the directory foo):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
In my php script I am displaying the values of $_SERVER['REDIRECT_QUERY_STRING'] and $_SERVER['REQUEST_URI']
If I request a URL like http://sitename/foo/bar the values as expected are bar and /foo/bar respectively.
This also works as expected for: http://sitename/foo/admin and http://localhost/foo/bar.
However when I try to access http://localhost/foo/admin (using localhost instead of sitename) the REQUEST_URI changes to /admin/?admin (this is how it displays in the address bar too, i.e http://localhost/foo/admin/?admin)
I searched for any .htaccess files that might be conflicting and also turned on mod_rewrite logging at level 6 but was unable to find any info.
I have no clue what might be causing it. It would be great if I could know what might be causing this, otherwise I might switch to nginx.
Thanks all for replies. This was some weird server configuration error, things seem to be fine on the new VM I installed. #Dan Grossman , thanks for your suggestion about using $_GET, some of my code is simpler than earlier.
Related
I have been looking at this issue for a few hours. I looked at many Stackoverflow posts, and I couldn't find the solution to my problem.
I scrapped my website a few months ago, and just started a new one shortly thereafter. However, my website does not seem to accept PHP. I don't know if the issue is with the server (when I renewed my hosting), or something else. Even calling an external PHP file, using the include function, does not work.
Anyhow, after placing the following code (along other simple coding), I see this in my PHP file when I browse the source code - meaning PHP is not working:
<?php phpinfo() ; ?>
Also, my htaccess file says the following (but I honestly have no clue what any of it means. Though I always hear people say that this file can make a difference:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
It is a bit hard to understand what the actual cause is. Do you run your own webserver? If so, is it apache httpd or IIS? If you have an external hosting partner, they should be able to help you.
Why do you have a htaccess file? You only need it for things like redirect rules. If you don't use that, delete this file or empty it.
Installation of LAMP stacks :https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04
I'm having some issues figuring out how to use an htaccess file. I've got apache/php installed on an ubuntu system and mod_rewrite is turned on (php_info() states that it's in the list of loaded modules). The web server works, displays html and php files, so I'm happy with that.
What I'm trying to figure out now is how to use an htaccess file properly. I created a directory, /data, with an index.php file in it. All I want it to do at the moment is just display the $_REQUEST variable so I can see if things are working the way I assume they should.
Example: If I type in the following URL: localhost/data/info1/ I want the htaccess file to access localhost/data/index.php?request=info1
However, no matter what I enter in to the htaccess file, I keep getting 404 errors, and I'd like to understand why.
Here's my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule data/(.*)$ data/index.php?request=$1 [L]
</IfModule>
I've made no changes to the config file, to activate mod_rewrite, I used the ubuntu a2enmod command so ubuntu did it for me. After that, I restarted apache.
What I can't figure out is why this doesn't work. My assumption is that there's still some sort of configuration I need to do on the server end, but I honestly don't know what. Is there any advice anyone can offer me?
Here's the fix:
RewriteRule ^data/(.*)$ data/index.php?request=$1 [L]
(You were missing a ^)
EDIT:
In the OP, you have another leading / in the URL example, in this case it'd be:
RewriteRule ^data/(.*)/$ data/index.php?request=$1 [L]
I have just had a problem with a Codeigniter site where, after the hosting company had migrated files to a new server, I could no longer navigate away from the home page. I Googled a forum with a similar issue and the answer was my htacess file. It was previously like this:
Options FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
and the given solution was to add a '?' character after 'index.php'. I did that and everything then worked OK.
I have tried hard to understand htaccess code and syntax and read many documents but they might as well be written in Chinese for all the sense I can get out of them. So can anyone explain why that additional '?' was needed and what it did? And if you can explain the rest of the code too I would be eternally grateful!
Your new host's php handler or webserver isn't setup to handle PATH INFO, or the stuff after the script (index.php) that's part of the actual path, e.g.
/index.php/this/is/part/of/the/path_info
The index.php script is executed, and everything after can be fetched via "PATH_INFO". If the server doesn't handle this, code igniter can optionally handle the path passed in as the QUERY STRING. Which are parameters that follow a ?.
None of this has anything to do with htaccess or mod_rewrite. It's just the way URLs and handlers work.
I built a website that works fine in development but not working correctly in production. I can't tell if the issue is related to the .htaccess or improperly configured vhosts. Essentially, the issue comes with the URL rewrites in my custom framework which relies on URL parameters called controller and action. Visiting a url such as domain.com/account/new would rewrite as domain.com/index.php?controller=account&action=new. Instead, when doing a dump of the $_SERVER global at the very beginning of my bootstrap (before any application logic has had a chance to mess with anything) I'm receiving this:
["QUERY_STRING"]=> string(41) "controller=index.php&action=index¶ms="
Where does that pilcrow (paragraph marker) come from? It doesn't matter what URL I try it still comes out that way.
NOTE: Other sites on the same server are able to do URL rewrites with no problem. It seems to be only this one.
I have verified mod_rewrite module is loaded and shows up in php -i / phpinfo().
Here is my .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+/)*favicon\.ico$ assets/img/favicon.ico [L,NC]
RewriteCond %{REQUEST_URI} ^/assets.*$
RewriteRule ^(.*)$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !favicon\.ico
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/)([^/]+)(/)(.*) index.php?controller=$1&action=$3¶ms=$5 [L]
RewriteRule ^([^/]+)(/)(.*) index.php?controller=$1&action=$3¶ms= [L]
RewriteRule ^([^/]+)(/) index.php?controller=$1&action=index¶ms= [L]
RewriteRule ^([^/]+) index.php?controller=$1&action=index¶ms= [L]
Dev Environment:
MacOS 10.7
Apache 2.2.22
PHP 5.4.15
Production Environment:
Linux (Kernel 2.6.32)
Apache 2.2.3
PHP 5.3.3
TL;DR; My .htaccess rules are rewriting incorrectly and including a pilcrow (paragraph marker) followed by "ms=". Is this a problem with my htaccess rules? Does this seem like a server issue? I'm tearing out my hair trying to figure this out.
EDIT #1
I just noticed that in my htaccess, something like this index.php?controller=$1&action=$3¶ms= contains "¶" which is the HTML entity for the pilcrow symbol which would make complete sense why it turns into that symbol followed by ms=. Now the bigger question is why would an htaccess process HTML entities?
EDIT #2
Ok, so I must be ignorant. It was displaying that because I was viewing it in the browser. When I went to "view source" it comes through as ["QUERY_STRING"]=> string(41) "controller=index.php&action=index¶ms=". Ok fair enough, now to figure out why it is catching the filename index.php in the $1 variable. Hmm.
I was unable to solve my issue so I rewrote everything to pass all values as a "resource" in my .htaccess like:
RewriteRule ^(.*) index.php?resource=$1
And then within the application logic I handled all of the processing. Thanks for viewing.
I can’t seem to make this work - and I’m pretty sure that the real problem is that I just starring myself blind on it, so I hope a pair of fresh eyes can help me out.
What I wan’t to do is have several applications attached to my system.
At this time, a website already exists in the root folder, but I wan’t some microsites/powerformats in a CI installation.
My mod_rewrite looks like this:
RewriteCond %{REQUEST_URI} ^/(powerformat1|powerformat2)/?$
RewriteRule ^(.*)$ powerformats/index.php/$1 [L]
Although, getting the CI index.php correctly, when trying to access example.org/powerformat1 or example.org/powerformat2 gives me CI’s 404 page.
It seems like whatever I try of rewrite rules I either get the 404 page or nothing at all.
Any insights?
-- EDIT --
What I believe is my problem is that CI actually gets the 'powerformat1' string passed as the first segment. That is what I need to avoid. But can't that be solved through mod_rewrite?
You could try link directly to the file with the appropriate query string instead
RewriteRule ^(.*)$ /powerformats/index.php?somequery=$1 [L]
(you may have to change the slashes, see below)
Or it may be this:
Accessing /powerformat1/
may be rewriting to
powerformats/index.php//powerformat1/
You could try
RewriteRule ^/(.*)/$ /powerformats/index.php/$1 [L]
or some other variation with slashes:
RewriteRule ^/(.*)/$ /powerformats/index.php/$1/ [L]
Did you miss RewriteEngine On ?