bytingo
bytingo

Reputation: 45

How to remove the page alias from url with htaccess?

I have created my own cms and i find the following issue. The pages links show as this:

https://www.example.com/page/contact-us

I want them to show as this, by removing the page alias:

https://www.example.com/contact-us

My Htaccess:

Options -MultiViews
RewriteEngine On

RewriteRule ^page/([\s\S]*)$ single-page.php?slug=$1 [L]

I have tried this but doesn't work for me

RewriteEngine On
RewriteRule ^([^/]*)$ /page/single-page.php?slug=$1 [L]

Upvotes: 2

Views: 235

Answers (1)

anubhava
anubhava

Reputation: 784908

RewriteRule ^([^/]*)$ /page/single-page.php?slug=$1 [L]

This won't work because it is forwarding all URIs to /page/ directory which obvious doesn't exist.

You should be using this rule:

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ single-page.php?slug=$1 [L,QSA]

Upvotes: 2

Related Questions