Dions
Dions

Reputation: 71

Htaccess new line crashes page

I recently tried to hide parameters from my url, using htaccess, ended up with a 500 Server Error.

Here is my .htaccess code:

<files .htaccess>
    order allow,deny
    deny from all
</files>
Options -Indexes +FollowSymLinks
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404.php
RewriteEngine On    
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteBase /
RewriteRule ^([^/]+\.php)/.* $1 [R=301,L]

RewriteRule ^buyit\.php$ /buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7 [L]

All I wanted to do is my page buyit.php?qsta=1&gid=11-64-36&qid=4&kaq=0&req=14&dlk=7 to show always as buyit.php keeping my variables intact and usuable for my php scripts. So I added this last line in htaccess but unfortunately it crashes.

Any ideas why, please?

Upvotes: 4

Views: 361

Answers (2)

Jon Lin
Jon Lin

Reputation: 143916

Your code is looping. This is because you can't match the query string in a rewrite rule, thus a URL like:

/buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7 

actually matches:

^buyit\.php$

So you need to add a condition to check that there isn't a query string. Change your last rule to this:

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^buyit\.php$ /buyit.php?qstart=1&gid=73161-645821-36&qid=4&daq=0&preq=14&clk=7 [L]

Upvotes: 2

Sak90
Sak90

Reputation: 590

Unfortunately, if you are passing values using GET method, you cant simply get rid off the parameters. However, you can remove the parameters name and make the url unique and NOT EASILY UNDERSTANDABLE by some one who doesn't know about the parameter names.

Try this htaccess code.

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /buyit.php?qsta=$1&gid=$2&qid=$3&kaq=$4&req=$5&dlk=$6 [L]

It converts your URL from this :

 http://yourdomain.com/buyit.php?qsta=1&gid=11-64-36&qid=4&kaq=0&req=14&dlk=7

to this

http://ydomain.com/1/11-64-36/4/0/14/7.html

which is also good for your SEO.

Upvotes: 1

Related Questions