1

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

4 Answers 4

1

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

3 Comments

Thank you man, this worked. :) Just one last part that may need fixing is the &action= it loads page?page=page&action=ACTION. However it works almost perfectly! this stuff is confusing, but I really like it. Any ideas on what to do next?
You're welcome. It shouldn't be doing that at all, because of the question mark after %2, which cancels the query string. Perhaps try clearing your cache?
Perhaps you could try using the query string discard flag instead of the question marks after %2?
0

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]

Comments

0

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]

1 Comment

mod_rewrite doesn't support query strings in rules. Please read the documentation before posting answers.
0

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]

2 Comments

This is working the most so far. website.com/gallery/?page=gallery is the result
@AustinWilley you sure you added the ? at the end of the rule's target?

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.