htaccess not working cant find file - php

RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^Download/(.*)/?$ ./downloadit.php?key=$1
RewriteRule ^Buy/(.*)/?$ ./buy.php?key=$1
RewriteRule ^Admin/(.*)/?$ ./admin.php?cmd=$1&key=$2
RewriteRule ^Admin/Edit/(.*)/?$ ./editfile.php?key=$1
I dont normally work with Apaches Rewrite Engine so Im not sure.
The Link I am Attempting to Parse is: http://Website.com/~User/Download/Testlicious/
I have 4 Files in total, all of them php files. I will like to rewrite directly to those files
But it keeps giving me a 404 cannot find /downloadit.php its in the same directory, same with the rest.
*UPDATE***
Since on on a Virtual Host, Duh. I need to Set my Rewrite Base Correctly, DOH
RewriteEngine On
RewriteBase /~GameCenter/
RewriteRule ^Download/(.*)/?$ ./downloadit.php?key=$1 [qsa]
RewriteRule ^Buy/(.*)/?$ /buy.php?key=$1
RewriteRule ^Admin/(.*)/?$ /admin.php?cmd=$1&key=$2
RewriteRule ^Admin/Edit/(.*)/?$ /editfile.php?key=$1
Now everything now Links and Works but the Problem now is that $0/$1/$2 dont trigger anything in the Files. Nothing Spits out in $_POST['key']
Now I guess Step 1 of the Problem is Solved. But now onto O_O STEP 2!!

You need to add the conditions in for each rule, and I'm pretty sure the RewriteRule pattern match requires a slash at the start too.
I've assumed the 4 PHP files are in the same directory that your .htaccess file is in.
The [L] tells mod_rewrite to stop parsing the .htaccess file for the current request:
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^Download/(.*)/?$ /downloadit.php?key=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^Buy/(.*)/?$ /buy.php?key=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# You've only specified one matching group here, so $2 will be blank
RewriteRule ^Admin/(.*)/?$ /admin.php?cmd=$1&key=$2 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^Admin/Edit/(.*)/?$ /editfile.php?key=$1 [L]

Related

htaccess adding .php to the end of $_GET variables when adding slash

I attempted to create a little bit of htaccess which alters a URL from something like
http://localhost/website/page.php?id=_abc-123
to
http://localhost/website/page/_abc-123
It works for the most part, in that I can visit the page without having trouble locating scripts and CSS files. However, if I try to echo out $_GET["id"], instead of getting _abc-123, I will get _abc-123.php.
This is what I have so far within my htaccess file:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
All help is appreciated,
Thanks.
Test movie page first, and test if file with .php exists:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
# remove extensions
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
First of all, if you are not sure where your error lies, you can try online tools for .htaccess like htaccess.mwl.be.
Obviously your first RewriteRule contitions are met, which results in your "error".
With the help of this tool and some knowledge about how regex work, we can fix your .htaccess to this:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
The only thing I changed is removing the "L" flag from your first RewriteRule, because its RewriteCond is met but we need it to go through the second RewriteRule.
For more information about the L-flag have a look at the documentation.

Weird .htaccess behaviour

this is my .htaccess-content:
RewriteEngine On
RewriteBase /
Options -Indexes -MultiViews
#Rewriting /profile.php?name=XY to /player/XY
RewriteRule ^player/([^/]*)$ /profile.php?name=$1 [L]
#Remove .php file ending
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
If I am browsing to my-domain/player/XY it redirects me to player.php?name=XY (and prints an internal server error because player.php doesn't exist) instead of showing the profile.
But if I change it to
RewriteRule ^player/([^/]*)$ /profile.php?name=$1 [L] and open my-domain/playera/XY it works fine.
Can you help me, please?
Not sure why you're getting that error since the first rule should match /player/XY. But you can probably add a few conditions to your php rule to ensure that it is rewriting correctly:
#Remove .php file ending
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]

Using RewriteEngine in PHP to read a file

recently I'm making a php application and I want some changes in my file.
So I use .htaccess and RewriteEngine and when I want to run my application it said 404.
Here is my code that I try and said 404 !!
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^access/(\d+)*$ show.php?address=$1&login=0
for example this my original address:
www.ex.com/show.php?address=5411496bc2b6c&login=0
And I want to change it to:
www.ex.com/access/5411496bc2b6c
Here is my question Am I doing something wrong in .htaccess ?
Try this rule:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^access/([^/]+)/?$ show.php?address=$1&login=0 [L,QSA,NC]
In your rule:
RewriteRule ^access/(\d+)*$ show.php?address=$1&login=0
Should be:
RewriteRule ^access/([a-z0-9]+)$ show.php?address=$1&login=0
(\d+) means digits and as it seems you need digits + lower alpha characters right?
Also SCRIPT_FILENAME will match executing script file name, so it will never be directory and you will also rewrite real existing directories, instead use REQUEST_URI or REQUEST_FILENAME
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
This works for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^access/([a-z0-9]+)$ show.php?address=$1&login=0 [L]

htaccess remove .php and keep query string

Here is my .htaccess file right now.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [NC,L,QSA]
This works in the fact that it makes my pages accessible when not using the .php extension.
Old = domain.com/test.php
New = domain.com/test
The bad thing is that when I send get data with the following link the data is not passed. I thought the QSA option did that, whats the deal?
domain.com/test?id=1
Matching the entire query string and appending it to your new URL using a back-reference should work.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
Here's a more simple solution. We use it on our team project. It will work not only in the root, but in any directory of your website.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If you are passing id then you have to use this code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^test/(.*)$ /test.php?id=$1
now you can access
domain.com/test?id=1
from
domain.com/test/1

.htaccess file extension and $_GET rewrite

i want to rewite these in my .htacces file:
/*.php -> /*(/)
(e.g. gallery.php to /gallery or /gallery/)
/snippets.php*?s= -> /snippets/*
(e.g. snippets.php*?s=test to /snippets/test or /snippets/test/)
my code so far:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^snippets/([^/\.]+)/?$ snippets.php?s=$1 [L]
the bugs that appear using my code:
/snippets/ and /snippets/test(/) will alert an 500 Error. /snippets works fine.
what am i doing wrong?
Like Micheal said, you need to change the order, however Michael didn't move the RewriteCond's, which result in the unexpected behavior.
RewriteEngine on
RewriteBase /
RewriteRule ^snippets/([^/.]+)/?$ snippets.php?s=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
I verified this code on my test server, just to be sure.
You almost have this correct. To take specific action for /snippets, it will need to come before the catch-all rule. Otherwise the first rule matches and routes to snippets/test.php which doesn't exist.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match snippets first...
RewriteRule ^snippets/([^/.]+)/?$ snippets.php?s=$1 [L]
# Then the catch-all for remaining matches
RewriteRule ^(.*)$ $1.php [L]

Categories