Austin Willey
Austin Willey

Reputation: 39

htaccess remove param name from url

I am trying to remove the get keys from my URL.

So https://www.website.com/?page=gallery would become https://www.website.com/gallery.

And if &action is there it should be like https://www.website.com/?page=gallery&action=view into https://www.website.com/gallery/view.

Of course, gallery, and view are dynamic - they define what page my site loads.

I have been trying all day, but to no avail. Can any of you help me out here?

My current .htaccess:

Order Allow,Deny
Allow from <MYIP>

ErrorDocument 403 /message.html

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

#Hide index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]


#Force www and https:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteRule ^(.*)$ https://www.website.com/$1 [L,R=301,NC]

#Gzip
<ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript
</ifmodule>
#End Gzip

Options -Indexes

Upvotes: 1

Views: 754

Answers (4)

Mike Rock&#233;tt
Mike Rock&#233;tt

Reputation: 9007

Give the following a try (just above your gzip section):

# Step 1: Redirect query-based URL to new one

RewriteCond %{QUERY_STRING} ^page=([^&\s]+)$
RewriteRule ^(?:index\.php|)$ /%1? [R=301,L]

RewriteCond %{QUERY_STRING} ^page=([^&\s]+)&action=([^&\s]+)$
RewriteRule ^(?:index\.php|)$ /%1/%2? [R=301,L]

# Step 2: Rewrite new URIs to query-based one
#
# Note how a dummy &r is used in the destination - this is
# to prevent infinite loops

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\s\/]+)/?$ index.php?page=$1&r [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\s\/]+)/([^\s\/]+)/?$ index.php?page=$1&action=$2&r [L,QSA]

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143866

Try adding this below the force www rule:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(?:index\.php|)\?page=([^&\ ]+)
RewriteRule ^ /%1? [L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?page=$1 [L,QSA]

Upvotes: 0

Dash
Dash

Reputation: 13

Haven't tested this, but based on what you're asking something like this should get you there?:

RewriteEngine on
# to convert from folders request query string index handler
RewriteRule ^/([^/\.]+)(/([^/\.]+))/?$ index.php?page=$1&action=$3 [L]
# or reverse from query string to folder paths (more limited/confining) 
RewriteRule ^/?(page=([^&]+)(&action=([^&]+)(&?(.*))$ /$2/$4?$6 [L]

Upvotes: 0

Richard Snazell
Richard Snazell

Reputation: 242

You could try using the QUERY_STRING condition - perhaps something like this:

RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^(.*)$ /%1/ [R=301,L]

RewriteCond %{QUERY_STRING} ^page=(.*)&action=(.*)$
RewriteRule ^(.*)$ /%1/%2/ [R=301,L]

Upvotes: 0

Related Questions