Rewrite get params with htaccess not working - php

I have been searching this for about 2 hours now and have tried every solution, but still cannot figure out what I am doing wrong. I'm working on writing an api. I'm trying to use "pretty" urls instead of having to pass params.
When I call myurl.com/categories it properly returns the entire list of categories.
When I call myurl.com/categories/1, which would then get the category with id=1, it returns a 404 error.
When I call myurl.com/categories?id=1 it successfully returns the category with id=1.
Here is my .htaccess
RewriteEngine On
RewriteRule ^categories/([0-9]+)$ /categories.php?id=$1 [L,NC]
RewriteRule ^([^\.]+)$ $1.php [L,NC]
Any ideas what I am doing wrong?

Change your .htaccess to this:
Options -MultiViews
RewriteEngine On
RewriteRule ^categories/([0-9]+)$ categories.php?id=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L,NC]

Try your rules this way and make the / optional as well.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^categories/([0-9]+)/?$ /categories.php?id=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [L,NC]

Related

Issue with .htaccess for friendly urls

This is my currently my .htaccess file
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
RewriteRule ^(.*)/edit/(\d+)?$ $1/edit.php?tag=$2 [NC]
As you can see it's set to hide the .php extension, but this then seems to break the edit rule. If I comment out
RewriteRule ^(.*)$ $1.php [NC]
The edit rule works fine, but I need both and cannot seem to get it to work, anyone see what the problem is and how to sort it.
[Edit]
I have a link like this and when all rules are active this is what doesn't work.
http://www.domainname.com/researcher/lists/edit/
and I get an 500 Internal Server Error.
Try rules as in your site root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteRule ^(.+)/edit(?:/(\d+))?/?$ $1/edit.php?tag=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

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.

Beautiful url with htaccess not working

I have been looking at a lot of htaccess code to fix this, but i am lost, I tried to change it arround 50 times but still with no result, if you can help me, it would be appreciated :
I am trying to create something like this :
www.domain.com/expositions/nameoftheexposition/
For now, I can easily do www.domain.com/expositions/ without any problem, but i can't seem to be able to access the next level.
here's my htaccess :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^expositions/([^/\.]+)$ expositions?name=$1 [L]
Change order of rules:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^expositions/([^/.]+)/?$ expositions?name=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Redirect /forgot-password/12345 -> /forgot-password.php?reset_key=12345

I'm attempting to set up .htaccess to allow for clean password reset URLs as follows:
The user is sent a link like:
www.url.com/forgot-password/12345
This should be rewritten as:
www.url.com/forgot-password.php?reset_key=12345
(and only for this page which is forgot-password.php - I want to keep the query strings for the other pages as-is.)
So far I have:
RewriteCond %{REQUEST_URI} !forgot-password
RewriteCond %{QUERY_STRING} (.)
RewriteRule ^(.*)$ /forgot-password.php?reset_key=$1 [L,QSA]
But it's not working. When I dropped the second line, it seems to trigger properly, but it's not passing the ?reset_key $_GET parameter in properly.
FYI, above this in my .htaccess, I already have the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Thanks in advance.
Keep your .htaccess like this:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^forgot-password/(.+)$ /forgot-password.php?reset_key=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [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

Categories