.htaccess Remove .php keep regular indexing - php

I'm using:
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
This makes it so I don't have to use ".php" at the end of my pages. However, if I don't specify an index it gives a 404.
Example:
example.com/content.php is directed to example.com/content
^ That works perfectly well.
However, if I want to go to a directory like example.com/users it gives a 404 error.
Only example.com/users/index works.
I want to be able to remove .php the way it is doing already, but I still want directories to work without specifying a page(automatically display index.php). I'm not very fluent in regex or htaccess coding so I have been unable to tweek this myself.
Any help would be appreciated. Thank you!

Replace your code with these 2 rules:
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Related

URL rewriting removes url ending

I've been trying to rewrite a specific file I have, /u.php/example to just /u/example with htaccess. The /example part I will get and use through php url parsing.
So, another question I read suggested using the following code
RewriteCond %{THE_REQUEST} /(u|a)\.php [NC]
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
But that does not preserve the /example from /u.php/example when rewritten, and causes a 500 error if I just enter /u/example.
Is there a work around for this? What could be the problem? I haven't been able to find a solution on Stack.
You need to capture and redirect the /example part from URI
RewriteCond %{THE_REQUEST} /(u|a)\.php/([^\s]*) [NC]
RewriteRule ^ /%1/%2 [L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/(.*)?$ /$1.php/$2 [L]

Htaccess re-write code conflict

I'm having a conflict with my htaccess file.
I´m using the following code to remove the php handles:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Everything works well EXCEPT for one subdirectory. When the php handle is removed, it turns out that the ¨handle-less¨ name shares the same name as en existing folder and as a result, the directory for that folder is being displayed, rather than the intended php page. I would rather avoid having to change the folder name especially if this is something that can be addressed from the htaccess file.
(I'm rather a beginner when it comes to coding... so I'm not sure how to best address this issue.)
Any help is appreciated. Thanks :)
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d said : if it is a directory, do not do the rewrite rule
Edit :
Could you try only :
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)/?$ /$1.php [NC]
This should rewrite all the requests with no dots inside to the php file

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.

RewriteRule blocking RewriteCond & RewriteRule after it

If this is a duplicate then I am really sorry, but I used google and also search here and could not find anything that would work for me. Maybe I just don't know what exactly to search for.
Anyway, back to my question:
I am trying to change old ugly URLs to nicer urls (wanted to have news/XXX/XY but relative links defeated me), so I wrote couple lines of code to my .htaccess and tested them (well, I actually used same code on my other page for redirect, so I know it does work). I get redirected from /news.php to /news, but the query string magic is not working at all...
RewriteRule ^news.php$ /news [L,NC,R=301]
RewriteRule ^news?$ /news.php [L,NC]
RewriteCond %{THE_REQUEST} /news\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-row-%1? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]
None of the RewriteRules after first one gets executed. But if I change my .htaccess like this:
RewriteRule ^news.php$ /news [L,NC,R=301]
RewriteCond %{THE_REQUEST} /news\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-row-%1? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]
RewriteCond %{THE_REQUEST} /news\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]
RewriteRule ^news?$ /news.php [L,NC]
then it works. I tried using /\?bla - /news\?bla - /news\.php\?bla etc. Nothing actually worked.
Does somebody have any Idea why its behaving like this and how to fix it? It looks like it does not recognize the news after it starts loading from news.php file.
I am completly lost, as I don't work with .htaccess that often. Any ideas?
I actually ended up using this:
#########################
# NICE URLS QUERIES #
#########################
RewriteCond %{THE_REQUEST} /news(?:\.php|)\?rowstart=([^\s]+) [NC]
RewriteRule ^ news-page-%1? [R=302,L]
RewriteCond %{THE_REQUEST} /news\(?:\.php|)\?readmore=([^\s]+)&rowstart=([^\s]+) [NC]
RewriteRule ^ news-%1-%2? [R=302,L]
RewriteCond %{THE_REQUEST} /news(?:\.php|)\?readmore=([^\s]+) [NC]
RewriteRule ^ news-%1? [R=302,L]
RewriteRule ^news-([0-9]+)-([0-9]+)?$ news.php?readmore=$1&rowstart=$2 [L,QSA,NC]
RewriteRule ^news-([0-9]+)/?$ news.php?readmore=$1 [L,QSA,NC]
RewriteRule ^news-page-([0-9]+)/?$ news.php?rowstart=$1 [L,QSA,NC]
#######################
# OTHER NICE URLS #
#######################
RewriteRule ^news.php$ /news [L,NC,R=301]
RewriteRule ^news?$ /news.php [L,NC]
The first thing you need to make sure you're doing is putting all the redirects first. That is, every rule that has the R flag needs to be before the rules that don't. But it looks like you're not matching the - style URLs to rewrite them BACK to the version with the query string. So I think you really want something like this (you can get rid of the .php extension while you get rid of the query string):
RewriteCond %{THE_REQUEST} \ /+news\.php(\ |$)
RewriteRule ^ /news [L,R=301]
RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?rowstart=([\ &]+)
RewriteRule ^ /news-row-%1? [L,R=301]
RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?readmore=([^&]+)&rowstart=([\ &]+)
RewriteRule ^ /news-%1-%2? [L,R=301]
RewriteCond %{THE_REQUEST} \ /+news(?:\.php|)\?readmore=([\ &]+)
RewriteRule ^ /news-%1? [L,R=301]
## Now, internally rewrite the request back to the query string and php extension
RewriteRule ^news$ /news.php [L]
RewriteRule ^news-row-(.*)$ /news.php?rowstart=$1 [L,QSA]
RewriteRule ^news-(.*)-(.*)$ /news.php?readmore=$1&rowstart=$2 [L,QSA]
RewriteRule ^news-(.*)$ /news.php?readmore=$1 [L,QSA]

htaccess code break down and some explanations

I've the following .htaccess file code
RewriteEngine On
RewriteBase /claimspro/
##-----Establish a custome 404 File not Found Page---
ErrorDocument 404 /claimspro/filenotfound.php
##-----Prevent Directory File listing in all folder---
IndexIgnore *
RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)&?([^\ ]*)
RewriteRule ^ %1/caseid/%2/?%3 [L,R]
RewriteRule ^([A-Za-z0-9]+)/caseid/([0-9]+)/ $1.php?caseid=$2 [L,QSA]
RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php
RewriteRule ^ %1/?%2 [L,R]
RewriteRule ^([A-Za-z0-9]+)/ $1.php [L,QSA]
The code above work the way I wanted it to be worked. It does change the url from this localhost/claimspro/afile.php?caseid=11 to localhost/claimspro/afile/caseid/11/
But when I add the another parameter i.e. /caseid/11/picid/12/ through the following way it behave weirdly.
Can I ask for some explanations of the code so can manage to get some result working in my favour.
RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)&?([^\ ]*)
RewriteRule ^ %1/caseid/%2/?%3 [L,R]
First if may I ask what is this at the end of the first line (above) &?([^\ ]*) how would I have to modify this to add another parameter such as RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)\&picid=([0-9]+)\&?([^\ ]*). What do I have to do here please if you could explain what I'm doing wrong here.
Secondly this line RewriteRule ^ %1/caseid/%2/?%3 [L,R] what is ?%3 [L,R] this %3 is representing what?
basically I want to add one more parameter in my htaccess file to cater the following
localhost/claimspro/anotherfile.php?caseid=11&picid=22
What additions should I make to my code?
Any idea?
Let me answer your 2 questions.
Answer to first question: the pattern &?([^\ ]*) is matching the rest of the query string (if present).
Answer to second question: ?%3 [L,R] is appending the captured data (from first question) as a query string (the R flag is used to redirect with a 302 by default).
If i had to do it, i would have written it that way
Options -Indexes
ErrorDocument 404 /claimspro/filenotfound.php
RewriteEngine On
RewriteBase /claimspro/
RewriteCond %{THE_REQUEST} /([^/]+)\.php\?caseid=([0-9]+)&picid=([0-9]+)\s [NC]
RewriteRule . %1/caseid/%2/picid/%3/? [R=301,L]
RewriteCond %{THE_REQUEST} /([^/]+)\.php\?caseid=([0-9]+)\s [NC]
RewriteRule . %1/caseid/%2/? [R=301,L]
RewriteCond %{THE_REQUEST} /([^/]+)\.php\s [NC]
RewriteRule . %1/? [R=301,L]
RewriteRule ^([^/]+)/caseid/([0-9]+)/picid/([0-9]+)/$ $1.php?caseid=$2&picid=$3 [L]
RewriteRule ^([^/]+)/caseid/([0-9]+)/$ $1.php?caseid=$2 [L]
RewriteRule ^([^/]+)/$ $1.php [L]
I can't explain the code because i'm not an expert in mode rewriting you can change your code withe the following code and it would work
RewriteEngine On
RewriteBase /claimspro/
#-----Establish a custome 404 File not Found Page---
ErrorDocument 404 /claimspro/filenotfound.php
#-----Prevent Directory File listing in all folder---
IndexIgnore *
RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)\&picid=([0-9]+)&?([^\ ]*)
RewriteRule ^ %1/caseid/%2/picid/%3/?%4 [L,R]
RewriteRule ^([A-Za-z0-9]+)/caseid/([0-9]+)/picid/([0-9]+)/ $1.php?caseid=$2&picid=$3 [L,QSA]
RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php
RewriteRule ^ %1/?%2 [L,R]
RewriteRule ^([A-Za-z0-9]+)/ $1.php [L,QSA]
Let me know if it works
Regards

Categories