Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccesss Remove the File Extension and Variables From the URL

I know this has been asked many times but no matter what I try I still cannot get my code to work. I have this .htaccess file and I want to redirect my code from this:

example.com/news/articles.php?id=21&artName=Hello-World

to this :

example.com/news/articles/21/Hello-World

Currently I have this code in my .htaccess file:

Options -MultiViews
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^news/articles/([\w-]+)/(.+) news/articles.php?id=$1&artName=$2 [QSA,L,NC]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule !\.php$ %{REQUEST_URI}.php [QSA,L]

This is the href link to that article:

<a href="https://www.example.com/news/articles/21/Hello-World">Article</a>

Whenever I try to go to that page. I get a "Error 404- Not Found" Page. I am new to editing the .htaccess file so please let me know what I am doing wrong in my code so I can see a successful rewrite in my link, thank you.

like image 651
Magical Tomato Avatar asked Dec 09 '25 11:12

Magical Tomato


1 Answers

You may try this at top of your .htaccess to disable mod_spelling choices:

<IfModule mod_speling.c>
   CheckSpelling off
   CheckCaseOnly off
</IfModule>

Options -MultiViews
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteRule ^(news/articles)/([\w-]+)/(.+) $1.php?id=$2&artName=$3 [QSA,L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

This will disable multiple choice option.

Summary
Requests to documents sometimes cannot be served by the core apache server because the request was misspelled or miscapitalized. This module addresses this problem by trying to find a matching document, even after all other modules gave up. It does its work by comparing each document name in the requested directory against the requested document name without regard to case, and allowing up to one misspelling (character insertion / omission / transposition or wrong character). A list is built with all document names which were matched using this strategy.

like image 114
anubhava Avatar answered Dec 12 '25 01:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!