Friendly URL's returns 404 - .htaccess

Hello everyone.
I have a problem with my htaccess file. I made friendly url's but server returns 404 error and i can't find where i made mistake in code. I spent 3 hours on it and still nothing. I'm begging you for help guys :D
Here's my code:
Options -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9-_/]+)$ /$1.php [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9-_/]+).(php|html|htm)$ /$1 [NC,R=301,L]
RewriteRule ^index.(php|html|htm)$ / [R=301,L]
Redirect is making good but server returns 404 error instead display website.
I want to make urls like example below:
From:
www.domain.com/customers.php
www.domain.com/products/number1.php
To:
www.domain.com/customers
www.domain.com/products/number1

Ok i solved this but now i have a redirect loop :(
New code:
Options -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9-_/]+).(php|html|htm)$ /$1 [NC,R=301,L]
RewriteRule ^index.(php|html|htm)$ / [R=301,L]

Try this
RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Related

htaccess rewrite rule, url doesn't work when i add a / at the end and it show error

Options +FollowSymLinks -MultiViews -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} ^themobilesapp.com$ [NC]
RewriteRule (.*) https://www.themobilesapp.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([A-Za-z0-9-]+)$ specification.php?url=$1 [L]
ErrorDocument 404 404error.php
</IfModule>**strong text**
When I open the URL https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443
it works fine but when i tried to open the URL by adding "/" in the last on the url like https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443/
I have tried many but it doesn't solve my problem.
I want my URL as https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443
With your shown samples, please try following Rules in your .htaccess file. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews -Indexes
DirectorySlash Off
<IfModule mod_rewrite.c>
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} ^themobilesapp\.com$ [NC]
RewriteRule ^(.*)$ https://www.themobilesapp.com/$1 [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ $1.php
RewriteRule ^([\w-]+)$ specification.php?url=$1 [L]
ErrorDocument 404 404error.php
</IfModule>
Also I saw in your previous edit that you need rule like rewriting to given php files with uri, you could cover all those rules with following single rule here:
RewriteRule ^([^/]*)/([\w-]+)/?$ $1.php?url=$2 [L]
This problem has been solved using the below code.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

301 Redirect Dynamic URL

OK so I have the following as part of the URL: /user/26872-essi/
and I'm going to final URL of /essi/
I currently have:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^user\/([0-9]+)\-.+[/]?$ /user.php?user_id=$1
So I tried:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^user\/([0-9]+)\-.+[/]?$ /$2/ [R=301,L]
RewriteRule ^(((?!\.|\/).){1,})\/?$ /user.php?username=$1
It doesn't seem to be passing the variable in. Any ideas what I could be doing wrong?
You may use these rules in your site root .htaccess:
RewriteEngine On
RewriteRule ^user/\d+-([\w-]+)/?$ /$1/ [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ user.php?username=$1 [L,QSA]

htaccess : access non www domain is showing index.php in url

I've tried every configuration possible but I just cant make it working.
I know the topic has come up number of times but I can't get the right solution for my case though it should be pretty simple.
When accessing www.example.fr, everything works fine and I get redirected to https://www.example.fr.
Now if I access example.fr then I get redirected to https://www.example.fr/index.php.I also just noticed that if I access https://www.example.com (same domain, different extension that belongs to me) I get redirected to https://www.example.com/index.php
Here is my .htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L,QSA]
RewriteCond %{HTTP_HOST} ^example.fr [NC]
RewriteRule ^(.*)$ http://www.example.fr/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
I want to get rid of that /index.php forever.
Any help would be appreciated !
You must place redirects before rewrites:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /web/
RewriteCond %{HTTP_HOST} ^example\.fr [NC]
RewriteRule ^ https://www.example.fr%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L,QSA]
</IfModule>

ReWrite Rule to a URL Friendly Page and User Profile

I want to use two RewriteRules to redirect Pages and Users to the right link but I'm coundn't achieve what I want, maybe someone could help me...
I want to redirect any Url Friendly page from:
http://www.example.com/my-url-friendly-page
to
http://www.example.com/index.php?cod=my-url-friendly-page
AND User Profiles from:
http://www.example.com/profile/username
to
http://www.example.com/profile/index.php?user=username
This is my htaccess, the first rule is working ok but I couldn't fix the second one...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?cod=$1
RewriteRule ^/profile/(.*)$ index.php?user=$1 [QSA,L]
</IfModule>
Can anyone help me with this ?
Reorder your rules like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^profile/(.*)$ index.php?user=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?cod=$1 [L,QSA]
</IfModule>

htaccess in codeigniter to clear index.php at url

I'm newbie with htacces..
I've file htaccess like this
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I called url like this without index.php it's work
http://localhost/url/
but when I try to call
http://localhost/url/controller/
and
http://localhost/url/controller/method/
the page back to xampp's home.
so what would I do? I want url without index.php.
thank you very much every advice.
Looks like you need something simple like:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|media|favicon\.ico|ipadicon\.jpg)
RewriteRule ^(.*)$ /index.php/$1

Resources