Htaccess RewriteRule causes 404 error. Why? - .htaccess

This is my code, it works almost perfectly, but if I write mysite.com/news/ instead of mysie.com/news, it gives me 404 error. What is the problem?
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/category\.php\?catid=(\S+)\s [NC]
RewriteRule ^ /news/categories/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/new\.php\?title=(\S+)\s [NC]
RewriteRule ^ /news/%1? [R=301,L]
RewriteCond %{REQUEST_URI} !/(exception\.php|admin) [NC]
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php/?\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^news/categories/([^/]*)$ category.php?catid=$1 [NC,QSA,L]
RewriteRule ^news/([^/]*)$ new.php?title=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Thank you very much!

Remove trailing slashes:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^ %1 [R=301,L]

Related

my .htaccess is not working on ubuntu 15.10

RewriteEngine On
RewriteBase /cpmsystem/
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
RewriteRule ^ %1/%2 [R=302,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)/{2,}[?\s] [NC]
RewriteRule ^ /%1/ [L,R=301]
RewriteCond %{HTTP_HOST} !^\. [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} system|application
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
#
my .htaccess is not working, can anyone please tell me what is wrong with my codes?
#

.htaccess redirect with php slug

My rewrite code is:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^products/([a-z0-9_-]+)$ products.php?slug=$1
But, this shows and internal server error for the products/slugname rewrite rule.
I've only used small letter, numbers and dash(-) in my slug id.
Try this :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ /$1.php [NC,L]
RewriteRule ^products/([a-z0-9_-]+)$ /products.php?slug=$1 [NC,L]

htaccess rewrite get url section not working

I am trying to rewrite the url example.com/parts/toolbucket/part.php?id=1 into example/toolbucket/part/1
I have most of the .htaccess working but the last part of the rewrite doesn't work.
This is my .htaccess
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/parts/([^\s]+)\s [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /parts/$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteRule ^toolbucket/([0-9]*)$ ./part.php?id=$1
Any ideas?
Because you are missing the directory part in the rule. If you want your URL to be this example.com/toolbucket/part/1
Replace this
RewriteRule ^toolbucket/([0-9]*)$ ./part.php?id=$1
With this.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^toolbucket/part/([0-9]*)/?$ /parts/toolbucket/part.php?id=$1 [L]

.htaccess change file name on link clicked

this is not working, which is supposed to be the easy part
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{THE_REQUEST} /rent\.php
#RewriteRule . RentCar/ [R=301,L]
RewriteRule ^RentCar/$ rent.php[L]
RewriteCond %{THE_REQUEST} /rent
RewriteRule ^RentCar/$ rent[L]
While more compliacted parts which are BEFORE this code work
RewriteCond %{THE_REQUEST} /rent2\.php\?r=([^&\s]+)\s [NC]
RewriteRule . CarRental/%1/? [R=301,L]
RewriteCond %{THE_REQUEST} /rent2\.php\?r=([^&\s]+)&w=([^&\s]+)\s [NC]
RewriteRule . CarRental/%1/%2/? [R=301,L]
RewriteRule ^CarRental/([^/]+)/$ rent2.php?r=$1 [L]
RewriteRule ^CarRental/([^/]+)/([^/]+)/$ rent2.php?r=$1&w=$2 [L]
It was resolved after some focus:
RewriteCond %{THE_REQUEST} /rent.php\s [NC]
RewriteRule . RentACar/ [R=301,L]
RewriteRule ^RentACar/$ rent.php [L]
Now Working
http://www.lebmotors.com/new/RentACar/

Htaccess redirect with many rules

I got rewrite rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|static)\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301] //add www before adress
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] //remove .php
RewriteRule ^ %1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-d //remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php
RewriteRule ^portfolio/([0-9]+)([^.]*)$ /portfolio-single.php?id=$1 [L]
But the last one dosent work, i want to have adress like www.mysite.com/portfolio/3-some-name => /portfolio-single.php?id=3
I dont know how to get i work.
(Sorry for my bad english)
Change order of rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|static)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteRule ^portfolio/([0-9]+) /portfolio-single.php?id=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Resources