Arne Schreuder
Arne Schreuder

Reputation: 183

.htaccess removing *.php throws away the parameters of a URL that is passed as a GET parameter

My request is quite simple. Using my current .htaccess conditions and rules as given here:

# Remove .php extension from URLS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# Redirect from *.php to URL without *.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

Problem is, when I pass a URL that contains *.php?param1=A&param2=B as a parameter, it throws away "?param1=A&param2=B"

For example: I want to redirect to: "/views/users/login.php?redirect=/views/home.php?id=1" Resulting in: "/views/users/login?redirect=/views/home", which throws away "?id=1", so now I can not access that parameter.

How do I rewrite my rules so that it keeps those parameters?

Any suggestions are welcome and much appreciated.

Update (2015-09-16):

Removing

RewriteRule ^(.*)\/index\.php$ $1 [R=301,L,NC]

As it is irrelevant.

Upvotes: 0

Views: 312

Answers (3)

Arne Schreuder
Arne Schreuder

Reputation: 183

I have solved this issue (to some agree) using the following code:

# Enable rewrite mod.
RewriteEngine On
RewriteBase /
Options +FollowSymLinks -MultiViews

# Redirect URLs that contain *.php to extensionless php URLs.
RewriteCond %{THE_REQUEST} ^(.*)\.php
RewriteRule ^(.+)\.php$ $1 [R,L]

# Resolve *.php file for extensionless php URLs.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php [NC,L,QSA]

# Resolve /views/* for URLs that do not contain views and needs it.
RewriteCond %{REQUEST_URI} ^/applications/(.*)$ [OR]
...
RewriteCond %{REQUEST_URI} ^/home [OR]
...
RewriteRule ^(.*)$ /views/$1

# Redirect URLs that contains /views/* to viewsless URLs.
RewriteCond %{THE_REQUEST} ^(.*)/views/(.*)
RewriteRule ^views(.*)$ $1 [R,L]

Upvotes: 0

Victor Radu
Victor Radu

Reputation: 2292

Hello you just need to add the query append marker like so:

RewriteRule ^(.*)\/index\.php$ $1 [QSA,R=301,L,NC]

that is "QSA"

Upvotes: 0

mgrueter
mgrueter

Reputation: 1420

You'll have to use urlencode to encode the URL into a parameter.

So when building the link or redirect, use:

redirect('views/user/login.php?redirect=' . urlencode('/views/home.php?id=1'))

btw: redirecting to a "controller" in a folder called "views" might be a bit confusing in a few month :)

Upvotes: 1

Related Questions