2

I've been working on a CMS recently, and wanted to remove the .php add a trailing slash and remove query strings e.g. page.php?page=contact to page/contact

So far my .htaccess file looks like this:

# This is a .htaccess file
# Don't edit it.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php\?page=([^&\s]+)&?
RewriteRule (.*)\.php$ /$1/%1/? [L,R=301]

RewriteRule ^([^/]+)/([^/]+)/$ $1.php?page=$2 [QSA,L]

other then that, I can't figure out how to shorten the query string. I've looked around on and couldn't find any other questions specific to this...

Thank you in advance.

1 Answer 1

1

I misunderstood you. I though you just needed to truncate the query string. This would pull the page parameter value from the query string and append it to the URL. ? truncates query string as before.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php\?page=([^&\s]+)&?
RewriteRule (.*)\.php$ /$1/%1/? [L,R=301]

RewriteRule ^([^/]+)/([^/]+)/$ $1.php?page=$2 [QSA,L]
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks, it worked. I added in: RewriteRule ^page/$ page.php - for that it loads the page.php as page/. Thanks for all your help. I selected your reply as the answer, but is it possible to remove ?page= and just keep the url as page/home ?
What's the URL you're using in the browser? /page.php?page=home ?? and at which place have you added your rule? Ordering is important.
I am using /page.php?page=home - i wanted to make the url /page/home ... so the url would be shorter and easier to understand/remember. so far with your help I have been able to remove the .php so the url looks like /page/?page=home
Does your htaccess look exactly like my answer? That should have taken care of it. If not can you update the question or open a new one with your current htaccess code.
This is what my .htaccess file looks like now. # This is a .htaccess file # Don't edit it. Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php\?page=([^&]+)&? RewriteRule (.*)\.php$ /$1/%1/? [L,R=301] RewriteRule ^([^/]+)/([^/]+)/$ $1.php?page=$2 [QSA,L] RewriteRule ^page/$ page.php I updated my topic.
|

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.