Using RewriteEngine in PHP to read a file - php

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]

Related

Mod Rewrite lost all GET parameters [PHP]

Today i start with my mod rewrite for the php script.
Some words to the setup
I use a subdomain e.g. test.testsystem.com which points to root/test/
So i have some multiple GET Parameters for my PHP script like the following:
1) https://test.testsystem.com/index.php?a=foo
2) https://test.testsystem.com/index.php?a=foo&v=bar
3) https://test.testsystem.com/index.php?a=foo&c=testcode
so with mod rewrite i want to have
1) https://test.testsystem.com/foo
2) https://test.testsystem.com/foo/bar
3) https://test.testsystem.com/foo/c/testcode
What i currently have is
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /test/
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?a=$1 [QSA]
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?a=$1&v=$2 [QSA]
For the first two URLs. Now it does no error 500 any more but the problem is i do not get any GET parameters in my php script.
Maybe someone can provide me here a working solution.
Regards
Assuming your test.domain.com is pointing to the /test folder , you can use something like the following in your /test/.htaccess .
Options -Multiviews
RewriteEngine on
#1)Rewrite /foo to /index.php?a=foo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?a=$1 [L]
#2)rewrite /foo/bar to /index.php?a=foo&v=bar
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?a=$1&v=$2 [L]
#3)rewrite /foo/bar/buzz to /index.php?a=foo&v=bar&z=buzz
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?a=$1&v=$2&z=$3 [L]
The RewriteConds are important in each rules to avoid rewriting your existing files and directories .

.htaccess URL needs to be created for sub folder view

I have enabled AllowOverride All in Apache configuration file.
This the URL users are typing in browser
http://hostname/system_name/dynamic_text/login
This needs to be working as below
http://hostname/system_name/index.php?id=dynamic_text
I have created rewrite rule as below. But its not working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z])$/login index.php?login=$1 [NC,L]
Can you suggest better way to fulfill this requirement
Problem is with your regex pattern as [a-z] only matches a single lowercase letter. You can use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/login/?$ index.php?login=$1 [NC,L,QSA]
This is created as per answers and comments above and works for me as expected.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/login/?$ index.php?login=$1 [QSA,NC,L]

Remove php extension from htaccess but keep PATH_INFO functional

I make URLs like http://localhost/index.php/add/
The /add/ after the index.php is $_SERVER['PATH_INFO'] and can be used in PHP coding.
I want to remove the .php from this URL using .htaccess and still keep using the $_SERVER['PATH_INFO'], no matter how may segments there are.
For instance,
http://localhost/index.php/add/cars/
http://localhost/index.php/add/cars/japan/
http://localhost/index.php/add/cars/japan/tokyo/
I also want a trailing slash at the end as in the examples above.
I can remove the .php extension using the script below, but I need a bit more than that.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
You can use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index(/.+)?$ index.php$1 [NC,L]
If you also use another name (and not always index). Change the last line:
RewriteRule ^([^/.]+?)(/.+)?$ $1.php$2 [L]
try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

URL Rewriting confusion

I got a webpage that has the following URL:
localhost/Minecraft-User-Info/player/?user=matthijs110
Now I want it to localhost/Minecraft-User-Info/player/matthijs110
So without ?user=matthijs110
I tried different ways, but it doesn't seem to work. Mod_Rewrite is enabled.
What can I do to get this to work?
Give this a try.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Minecraft-User-Info/player/(.+)$ /Minecraft-User-Info/player/?user=$1 [L]
Edit:
Use this since it's in a sub directory.
RewriteEngine On
RewriteBase /Minecraft-User-Info/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^player/(.+)$ player/?user=$1 [L]
Updated to show my original answer and included the edit per users comment about where htaccess is located.
Try this instead. Minecraft-User-Info/player/username Redirects to Minecraft-User-Info/player/index.php?user=username
Tested on http://htaccess.madewithlove.be/ instead of setting up my own server for it.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Minecraft-User-Info/player/(.+)$ /Minecraft-User-Info/player/index.php?user=$1 [L]
Try this
RewriteRule ^Minecraft-User-Info/player/([a-zA-Z0-9_-]+)\$ index.php?playerId=$1

htaccess not working cant find file

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]

Categories