Rewrited URL giving 404 error - .htaccess

I'm using mode rewrite to make seo friendly urls. Please see the code below
Options +FollowSymLinks
RewriteEngine on
RewriteRule discover/([0-9]+)$ discover.php?page=$1 [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Code work in my local host but in the hosting server it doesn't work.
If i added with the file extension it works in the server but without the extension it doesn't work.
Ex: below works
RewriteRule discover-([0-9]+)\.html$ /discover.php?page=$1 [L]
Any pointers on how to make the 1st code it work? Appreciate your help.

Try this too,
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^discover/([0-9]+)$ discover.php?page=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Turn off MultiViews option and check for existence of .php file before adding .php in a request:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule discover/([0-9]+)/?$ discover.php?page=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

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

.htaccess /subdirectory/ -> /subdirectory/index

I'm sorry if the topic with same question already exists, but I couldn't find it out.
So. My problem is, that I've a webpage in subdirectory; like..
/var/www/example/
Now, what I really need:
when I try, to get on the site from URL like: www.example.com/examplesubdir/ ; dirname($_SERVER['REQUEST_URI']) isn't returning anything.
That's why I want to rewrite it from www.example.com/examplesubdir/ to www.example.com/examplesubdir/index
My current .htaccess file looks like this:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9]{18})_([A-Za-z0-9]{3,4})$ show_file.php?f=$1&t=$2 [L,QSA]
RewriteRule ^([A-Za-z0-9]{18})$ show_file.php?f=$1 [L,QSA]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Thank you, and appreciate any help.
RewriteCond is only applicable to next RewriteRule only.
Replace your /var/www/.htaccess code with this:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([A-Za-z0-9]{18})_([A-Za-z0-9]{3,4})$ show_file.php?f=$1&t=$2 [L,QSA]
RewriteRule ^([A-Za-z0-9]{18})$ show_file.php?f=$1 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [NC,L]

RewriteRule for Pretty URLs not working

The URLs of my site are of the type
http://localhost/cms2/pages.php?mpage=2
I have written the following .htaccess in order to create pretty URLs, but nothing happens
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)\/$ pages.php?spage=$1 [NC]
Mod-rewite is ennabled and other rewrite rules I've tested work. For example, I have a file called "contact-us.php". I can make it look "mysite.com/contact-us" using the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)\.html$ $1.php [nc]
Any help would be appreciated.
Thanks in advance.
Sonia
Not working because your regex is incorrect. Use this rule in your root .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/([^./]+)/?$ /cms2/$2.php?spage=$1 [L,QSA]
This will rewrite a pretty URI: /3/page/ to /cms2/page.php?spage=3

Remove php file extension

I am trying to implement url this way localhost/mysite/contact/ without php extension and adding a trailing slash at the end but it is not working.
This is the code sample:
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
How can I implement the following:
localhost/mysite/page/
Any help please?
Thank you.
Ensure you have mod rewrite enabled and loaded
Eg
RewriteEngine on
this will work without the file v system checking overhead
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /mysite/
RewriteRule ^foo/?$ bar [L,NC,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/mysite/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

how to create friendly url in my directory using php

I use these type of URL:
http://test.com/rest/api.php?action=test
But I need these type URL:
http://test.com/rest/api/action/test
I try to .htaccess file these code here
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ api.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ api.php [QSA,NC,L]
Put this code by relacing your code your .htaccess under DOCUMENT_ROOT/rest directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /rest/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([^/]+)/?$ api.php?action=$1 [QSA,NC,L]
This should fix it:
RewriteEngine On
RewriteRule ^rest/api.php?action=test$ /rest/api/action/test [L]
Your goal can be achieved by using regular expressions with syntax like :
RewriteRule ^rest/api/action/(\d+)*$ ./rest/api.php?action=$1
try read these articles :
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/ and
http://net.tutsplus.com/tutorials/other/the-ultimate-guide-to-htaccess-files/

Resources