BDP
BDP

Reputation: 37

I am trying to achieve a directory URL using parameters with htaccess

I would like to show certain content on my website. I choose the content based on a URL parameter, like page=about. This works well, and my URL looks like example.com/mysite/index.php?page=about. My issue is that I would like my URL to read example.com/mysite/about, where the last part is the name of the page parameter.

I am attempting to do this through htaccess. This is my exact code:

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

I see no change on my page nor to the URL structure. The URL still reads ...index.php?page=about. Am I doing something wrong? I tried this a bunch of different ways and I still get the same result. I also tried simply doing this, with no change:

RewriteEngine On
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ index.php?page=$1 [NC]

I didn't think it was working, so I tried a redirect to some other site and it worked. Therefore, the htaccess file is getting read and able to function.

Any assistance would be helpful!

Thank you, BP

Upvotes: 0

Views: 51

Answers (1)

anubhava
anubhava

Reputation: 785551

You may use these 2 rules in your site root .htaccess:

RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(mysite)/index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/mysite/([^/]+)/?$ [NC]
RewriteRule ^ index.php?page=%1 [L,QSA]

Upvotes: 1

Related Questions