partseven
partseven

Reputation: 61

htaccess how to short url with get

I want to short my url from this:

https://example.com/?page=home

to

https://example.com/home

but I get this error:

Not Found
The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

My index.php is on the folder: /

My pages like home.php etc are on the folder: /pages

My code

RewriteEngine On
RewriteBase /

# Prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

# Rewrite if not index.php page
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

# Change url from page={something} to {something} and redirect
RewriteCond %{REQUEST_URI} /index.php
RewriteCond %{QUERY_STRING} page=(.*)
RewriteRule . %1? [L,R]

File-structur

/
-index.php (File)
-.htaccess (File)
-Images (Folder)
-Css (Folder)
-Pages (Folder)
 --Home.php

Upvotes: 1

Views: 200

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133710

1st solution: Based on your shown samples, could you please try following. This one considers that you are hitting url https://example.com/home in browser.

RewriteEngine ON
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?page=$1 [L]


2nd solution: OR in case you are hitting https://example.com/?page=home in your browser then try following.

RewriteEngine ON
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^/?$ https://%{HTTP_HOST}/%{QUERY_STRING} [R=301]
RewriteRule ^(.*)/?$ index.php?page=$1 [L]

NOTE: Please make sure you are putting only 1 of the rules set at a time, as they are written for different purposes.

Upvotes: 2

Related Questions