HTACCESS Works, But php $_GET Does Not - php

I have read through almost every post regarding this issue, and nothing seems to work.
I have successfully rewritten the below code, and it works just fine. The .htaccess file is in the /~user/business/ folder. I use RewriteRule ^index/([0-9]+)/?$ /index.php?id=$1 [QSA,PT] while accessing http://localhost/~user/business/index/155/. However, when I try to get the variable in php, it returns nothing. The rewrite works as it does load index.php, but no get variables at all.
My main goal, I want to access the id from the code above, but nothing shows up. <?php echo $_GET['id']; ?> Even when I try this print_r($_GET); the array of all get variables, it returns an empty array. I also do not want to append the query to the end of url at all.
Lots of posts have answers that work according to responses, and even when I copy them word for word, letter for letter, changing the directories and addresses, nothing seems to work.
I am using Apache 2, set up on a MAC as localhost. Is there something wrong, maybe in the php.ini file?

After many times trying, as much as reconfiguring the entire virtual host setup on my mac, I realized that it was a very simple error once I realized it, and it had nothing at all to do with my .htaccess file.
My file name was index.php and my rewrite was RewriteRule ^index/([a-zA-Z0-9_-]+)/ index.php?id=$1 [NC,L] and for some reason, it will not let me use ^index if there is a file called index.php. I can name ^index to ^page or ^blue or anything else but not ^index and it will work fine.
What I realized was that my /index.php page can be loaded simply by typing /index. And therefore, was not actually a redirect, it was the stand alone page.
In order to fix this I had to add a few lines of code to my vhost.conf or my user.conf file under <Directory "/Users/User/Sites"> file using the terminal.
sudo nano /private/etc/apache2/extra/httpd-vhosts.conf
and in this, the code I had to removed MultiViews from the options.
<Directory "/Users/User/Sites">
Options Indexes FollowSymLinks SymLinksifOwnerMatch MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
so it now looks like this
<Directory "/Users/User/Sites">
Options Indexes FollowSymLinks SymLinksifOwnerMatch
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Once I did so, /index.php page can no longer be loaded by typing /index. And therefore, I could use the code I originally used in my .htaccess file. And all is solved in paradise. It took a lot of very hard digging and one major headache.
Thanks for your help guys.

Related

Friendly URL in PHP and htaccess

I have a website built in PHP. Currently my URLs look like:
http://www.domain.com/web/views/site/event.php?id=1&name=Test
I want them to be like:
http://www.domain.com/event/id/1/name/Test
How can I achieve this? I have tried multiple tutorials and have checked for answers in stackoverflow but have not been able to find a proper solution.
Any thoughts?
Thanks in advance.
Create an .htaccess file and add the following lines:
RewriteEngine On # Turn on the rewriting engine
RewriteBase /
RewriteRule ^event/id/([A-Za-z0-9-_]+)/name/([A-Za-z0-9-_]+)/?$ web/views/site/event.php?id=$1&name=$2 [NC,L]
Two things that you should keep in mind:
.htaccess should be located at your root directory
Make sure that apache has the following directive regarding your root directory:
AllowOverride All
This is how is done:
If you have root access to your server, edit the httpd.conf file, find the root <Directory> line, and change it to this:
<Directory />
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
If you don't have root access, ask your server administrator to do that for you.

Allow access to a specified folder .htaccess 500 Internal Error

I tried to allow access to a directory by using the .htaccess file.
This is my .htaccess:
<Directory Bilder_Team>
Options Indexes FollowSymLinks
AllowOverride None
</Directory>
The directory is "Bilder_Team". If I open the link to this directory, it shows me
500 Internal Server Error
How can I fix this?
Use this htaccessCheck for future checks or problems. Your problem as you could see, once checked, is that you are not allowed tags such as <Directory> in .htaccess files.
Instead put the .htaccess directly into the Build_Team directory and leave off the surrounding tags.
Possibly, a bigger problem is the use of AllowOverride None, which disables the use of .htaccess files and, obviously, has no part of any .htaccess file. AllowOverride directive can only be part of the main config file!
As shown the snippet is correct iff put in the httpd.conf file (name could vary). Which actually, if possible, is the better way to do it.
For more information about security concerns, you could refer to this StackOverflow answer.
Hope this helps!

PHP script in aliased directory mistakenly handled by WSGI when using mod_rewrite

all. So I'm running Apache 2.2. I have a single VirtualHost that's used for a Django application (by way of mod_wsgi) as well as a PHP one that lives in a subdirectory. Normally, this is no problem. You can just Alias /subdir /path/to/phpapp followed by WSGIScriptAlias / /path/to/django.wsgi.
But, the complication is that this PHP application uses mod_rewrite to implement "Fancy URLs," which just means that '/subdir/foo/bar' will get rewritten to something like '/subdir/index.php?path=foo/bar'.
Here's my configuration:
<VirtualHost *:80>
ServerName [snip]
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
Alias /phpapp/ /home/ubuntu/phpapp/
<Directory /home/ubuntu/phpapp>
Order allow,deny
Allow from all
RewriteEngine on
RewriteCond $1 !^(index\.php|img|bin)
RewriteRule ^(.*)$ index.php?__dingo_page=$1 [PT]
</Directory>
WSGIDaemonProcess foo user=ubuntu
WSGIProcessGroup foo
WSGIScriptAlias / /home/ubuntu/djangoapp/apache/django.wsgi
<Directory /home/ubuntu/djangoapp/apache>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
The problem is that whenever a rewrite takes place (e.g., I try to go to /phpapp or /phpapp/foo) the request gets handled by WSGI and I see my Django site's 404 page. If the address passes through (e.g., I go to /phpapp/index.php) then it is handled by PHP and works normally. I thought maybe that adding the [PT] flag to my RewriteRule would fix this problem, but it doesn't seem to have any effect on which handler is chosen. I've also tried SetHandler application/x-httpd-php in the Directory section for the PHP app.
I need the Django application to continue to handle any URLs that aren't specifically aliased to something else. There must be a way to make this work! Thanks.
Using /phpapp wouldn't ever work because you have a trailing slash on that path for the Alias directive. Use:
Alias /phpapp /home/ubuntu/phpapp
By rights that Alias directive should then take precedence over WSGIScriptAlias for the sub URL.
I would suggest you enable logging by mod_rewrite and verify what the URL is being written to. So long as the rewritten URL still sits below /phpapp, it should be fine.
The alternative is to not use WSGIScriptAlias, but use the scheme as outline towards the end of:
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive
That allows you to set things up so that the Python web application will only be used as a fallback if no static resource, including PHP, could be mapped.

URL path redirects to a single php file

Want to know if it is possible to get different URL paths to redirect to a single php file which i can pull the name from the URL.
i.e. If i have www.example.com/username or www.example.com/username2 or www.example.com/username3
I want it to redirect to a username.php file which then i can get the username from the URL and load the information from the database.
If not does anyone have any ideas on how i can do something similar. tumblr do something similar as u can just type in a user-name in the url and it loads their blog.
Yes of course it's possible thanks to URL rewriting! Check out Apache mod rewrite.
For your information, a rewrite rule like you want may look like:
RewriteEngine On
RewriteRule ^/username(.*) /username.php?user=username$1 [QSA,L]
I'm pretty sure this will work. Just activate the mod_rewrite in your server.
when u want to open www.example.com/page/username
web server will redirect to www.example.com/index.php?username=username1
with that you can give a page to each user
actually they are pages like
www.example.com/index.php?username=username1
www.example.com/index.php?username=username2
www.example.com/index.php?username=username3
with rewrite engine they can be accessible like this.
www.example.com/page/username
www.example.com/page/username2
www.example.com/page/username3
.htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^page/(.*) /index.php?username=$1 [QSA,L]
Yes, this is called a FrontController Pattern and is usually achieved with a rewrite-rule. An example for a slightly different method for Apache2 would be:
<VirtualHost *:80>
ServerName quickstart.local
DocumentRoot /path/to/quickstart/public
SetEnv APPLICATION_ENV "development"
<Directory /path/to/quickstart/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This one is taken from the Zend Framework Manual, where you find a practical example of the FrontController Pattern. Basically, all requests are send to one file (in this example index.php) and handled from there, usually by routing the request to another controller.

How can I add one line into all php files' beginning?

So, ok. I have many php files and one index.php file. All files can't work without index.php file, because I include them in index.php. For example. if somebody click Contact us the URL will become smth like index.php?id=contact and I use $_GET['id'] to include contacts.php file. But, if somebody find the file's path, for example /system/files/contacts.php I don't want that that file would be executed. So, I figured out that I can add before including any files in index.php line like this $check_hacker = 1 and use if in every files beginning like this if($check_hacker <> 1) die();. So, how can I do it without opening all files and adding this line to each of them? Is it possible? Because I actually have many .php files. And maybe there is other way to do disable watching separate file? Any ideas? Thank you.
You could put your index.php alone in your web directory. And put all the files it includes in another non web directory.
Let's say you website http://www.example.com/index.php is in fact /path/to/your/home/www/index.php, you can put contact.php in /path/to/your/home/includes/contact.php. No .htaccess, rewrite, auto appending. Just a good file structure and a server configured like needed.
Edit to detail my comment about using xamp :
In your httpd.conf file, add something like this :
<Directory "/path/to/your/site/root">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>
<VirtualHost *:80>
DocumentRoot /path/to/your/site/root
ServerName www.example.org
</VirtualHost>
Then in your windows hosts file (in C:\Windows\System32\drivers\etc), add this line :
127.0.0.1 www.example.com
I would highly recommend to use the .htaccess file to rejects all requests for files diffrent to index.php but I am not quite sure how to do that propperly.
This might work (can't test it now) but it will also block requests to css, js and so on:
order deny,allow
<FilesMatch "\.php">
deny from all
</FilesMatch>
<FilesMatch "(index.php)">
allow from all
</FilesMatch>
If someone knows the right solution, please edit my answer.
You might check this question: Deny direct access to all .php files except index.php
So you might have a FilesMatch only for php files in addition to the index.php rule.
EDIT: The new version of the code seems to work.
In response to Kau-Boy:
Place all your php files (except index.php) in a new directory and put the .htaccess file with the following contents:
deny from all
Make sure you don't put any images/css/jscript resources in this directory, because they will be blocked as well.
I'd use mod_rewrite in this case (if you are using Apache). It's much cleaner solution than writing gazillions of useless ifs in PHP.
This way, if someone wanted to "hack it" and tried /system/files/contacts.php, it'd redirect them to index.php?id=contact or whatever other site.
In your php.ini or in you htaccess set the following variable:
auto_prepend_file="[path to some .php file]"
This will include a header file of your choice that will be included before all php scripts on the system.
The php.ini directive auto_append_file, will create a footer that is included at the end of all PHP files on the system.
Check out the technique at http://www.electrictoolbox.com/php-automatically-append-prepend/
RewriteCond %{REQUEST_URI} system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
Will redirect any attempt to system folder back to root!

Categories