htaccess remove .php and keep query string - php

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

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.

Error getting value of a query string

I removed all files with .php extension on my project folder using this .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
Now, for example, home/product.php becomes home/product.
However I have a problem, I have a url with a query string like this - "home/product_details.php?id=$id". I can only access "home/product_details". I want it to be like "home/product_details/$id".
Please how do I do it and how can I get the value of $id?
I would usually do:
$id = $_GET['id'];
I think I need to adjust something in the .htaccess but I don't know.
Thank you.
You can use the following rules :
RewriteEngine on
#remove .php exten
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*?)/?$ $1.php [L]
#rewrite /file/foobar to /file.php?id=foobar
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+)/?$ /$1.php?id=$1 [L]

htaccess already stripping ".php" but need a sub page with url query

Here's what I have so far...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
So site.com/page works to view site.com/page.php.
What I want to add to this is on one particular page where there is a query string...
example: site.com/song.php?name=Song-Name -> I want it to be site.com/song/Song-Name.
I know there are a lot of post similar to this and I've looked through a ton but just don't understand quite how to accomplish this.
You will need a new rule to handle /song/ URIs:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^song/(.+)$ song.php?name=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ $1.php [L]
I have also corrected your 2nd rule since query string is already appended to target.

htaccess for rewrite URL is not working for my website

I have dynamic URL website (irasol.com) while i navigate to menu the url shows like
http://irasol.com/index.php?id=1
I want url like this
domainname/home
domainname/aboutus
domainname/contactus
domainname/apply
home, aboutus, contactus, apply are menu name it is already in database.
my htaccess file is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.php$ /index.php?id=$1 [L]
Use this instead:
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?id=$1 [B,L]
Explanation
The first three conditions make sure that domainname/aboutus is not a real file, so that we don't rewrite files that already exist.
Options -Multiviews removes a number of potential problems
In your current code, get rid of the .php in your pattern:
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
You are not matching .php extensions in the request. You are only routing matches to a query string on a real .php extension
As for a better solution:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [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]

Categories