Pretty URL .htaccess gives 404 not found - php

I'm really not that good at writing htaccess. It looks like gibberish for me. :-)
So what we wan't to do, is to remove the .php extension to the URL.
Here we have our script
RewriteEngine On
RewriteBase /
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
RewriteRule ^help/([A-Za-z0-9_]+)/?$ help.php?artid=$1 [NC,L] # Process parrots
RewriteRule ^signature/([A-Za-z0-9_]+)/?$ signature.php?artid=$1 [NC,L] # Process parrots
RewriteRule ^signatures/([A-Za-z0-9_]+)/?$ signatures.php?artid=$1 [NC,L] # Process parrots
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
I really cant find the issue here.
The issue is, that when i f.x goto localhost/secure/profil it gives me a 404 not found. But when i comment ALL the .htaccess out, and goto localhost/secure/profil.php it works.
The exact url when htaccess is turned on looks like this:
http://localhost/secure/profil/
Maybe somebody can see what i'm doing wrong here?
Any help would be appreciated!
Kind Regards!

Related

.htaccsess rewrite rule for only one page

I am not familiar with the rewrite rules..
I am trying to get
export.php?type=pdf
to be able to be
export.php/pdf
I already have rewrite rules to remove .php from files..
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
so really it would be.
export?type=pdf
to
export/pdf
The issue I'm having is how do i go about doing this for JUST the export.php file ? I know its using Reg-ex for the rules, but i don't know enough to make a new rule, i have tried to Google it and find an answer on how to do it, but the issue is I'm not sure what this is called other than ".htaccess rewrite rule"
Try below rule after all your rules,
# incoming url is neither a file nor a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+)/?$ $1.php?type=$2 [L]

Pretty URL htaccess

I have the below url that I want to prettify. Any help appreciated.
From;
http://www.example.co.uk/search-menu?r_id=1&name=testname
To
http://www.abklab.co.uk/search-menu/1/testname
I have tried below but getting Internal Server Error
# external redirect from actual URL to pretty one (WEB)
RewriteCond %{THE_REQUEST} \s/+search-menu(?:\.php)?\?r_id=([^\s&]+)&name=([^\s&]+) [NC]
RewriteRule ^ /search-menu/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one (WEB)
RewriteRule ^search-menu/([\w-]+)/([\w-]+)/?$ search-menu.php?r_id=$1&name=%2 [L,QSA,NC]
FYI: Below rule removes .php extension site wide;
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Try this in your htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} /search-menu\?r_id=([^&]+)&name=([^\s]+) [NC]
RewriteRule ^ %1/%2? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search-menu/([^/]+)/([^/]+)/?$ search-menu.php?r_id=$1&name=$2 [NC,L]
Found something for you - the wizard will help you make your URL perfect
http://www.generateit.net/mod-rewrite/index.php
Add this line to your .htaccess file. It will take the variables and make is nice and pretty:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ /search-menu?r_id=$1&name=$2 [L]
Keep in mind this is basic and you will need to modify it to your addresses but it is just to give you the idea of what you need to do.

htaccess doesn't removing php extensions

I have a php project running. I am running my project on localhost. I want to remove php extensions from url in addressbar. I try several htaccess scripts but none worked some shows 404 not found error or doesn't work at all.
directory where my php project is D:\VertrigoServ\www\gotmation
I am using this htaccess script
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
# RewriteRule ^ %1 [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/gta/$1.php -f
# RewriteRule ^(.+?)/?$ /gta/$1.php [L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I am newbie php programmer doesn't have much experience of this stuff. Any help will be appreciated.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This must work... you might have any other issues... do you have permitted .htaccess in apache config file???
try:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
These rules need to be in your document root.

Hide extension of php files and hide index.php with htaccess

This has been asked a thousand times, but not exactly how I want it. I have tried combining different solutions, but my .htaccess doesn't seem to do what it is supposed to.
# Not sure what this does?
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
# Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z](3,9)\ /([^/]+/)*index\.[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.example.com/$1? [R=301,L]
Now my pages correctly change from domain.com/page1.php to domain.com/page1, however something goes wrong with domain.com/index.php.
I am testing this locally and when going to localhost/project everything works fine (the index.php opens, but you don't see that in the url) but when you explicitly navigate to localhost/project/index.php you are returned to the very root, i.e. localhost (which hen returns to http://localhost/xampp/splash.php). Of course, this is not what I want. I want localhost/project/index.php to return to `localhost/project/ยด.
An additional question, though: how do rewrite rules influence search engines. Will the pages (ie contact.php, about-us.php and so on) still be indexed?
Extra +1 and kudos for he or she who gives a detailed breakdown of what each line/rule in the htaccess in their answer does. I am still learning .htaccess, so every detail is important and relevant to me!
Lot of comments in this code in question seem to be mine :)
Anyway you can use this code to fix your issue:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /project/
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+project/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1%2 [R=302,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/project/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Update: For using in DocumentRoot:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1%2 [R=302,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

hiding .php address bar object not found

I have a simple problem, i tried to remove .php on the address bar using this code below
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally redirect /dir/foo/ to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
and it works but eventually it keeps on displaying "OBJECT NOT FOUND" how do i fix this problem?
You may try to use this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Example: Your page is: www.yoursite.com/yourpage.php . Now you can access the same page without ".php".
ALSO
You can try to remove [R] permanent redirect (to remove your error), and mantain the extension in rule.
I hope this will help you!
If all you want to do is remove the .php extension from your URLs, just use Options +MultiViews, no need for the rewrites.

Categories