4

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

3 Answers 3

6

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]
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

3 Comments

what if this variable always changes or there are different variable names, also I would need to pass data on other pages as well. There isn't just a rule saying take out the .php and leave everything else?
you can also pass id=2, id=3... so on.. to access id=2, use domain.com/test/2
and if you are different variable, or 2 variable, just change rule to RewriteRule ^test/(.*)/(.*)$ /test.php?id=$1&othervariable=$2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.