PHP/.htaccess: removing php extension from url - php

I was using .htaccess code to remove .php extension for all my web pages. Here's the code I use:
RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
It doesn't seem to work. I think I'm missing something. When I type www.mysite.com/about/ to get www.mysite.com/about.php it returns error 404 (page not found). Can someone please shed some light.
Thanks,
Paul G.

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# If folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# and file exist
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
# uncomment the below rule if you want the "/" to be required
# otherwise leave as is
# RewriteRule ^([^/]+)/$ $1.php [L]
# internally show the content of filename.php
RewriteRule ^([^/]+)/?$ $1.php [L]
The above rule will:
will not redirect if a folder exist
will not redirect if the file does not exist
will redirect what comes before the / if one is present as the file name
So it will work for all these examples:
http://domain.com/about/
http://domain.com/about
http://domain.com/contact/
http://domain.com/contact
If you want you can remove the ?, like the commented rule, to make it accept only URL's that end with a /.
http://domain.com/about/
http://domain.com/contact/
Now these are important step for the above to work:
It must go into the .htaccess on your root folder for example /home/youraccount/public_html/.htaccess
The Options before the rewrite rule are very important specially -MultiViews
The file must exist on the same place the .htaccess is for example in your case the about.php file
The PHP must be working obviously.

It seems like the slash at the end of your rule might be there, or it might not. Adding a ? makes it optional, so that mysite.com/about and mysite.com/about/ will both match.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-\s]+)/?$ /$1.php
It's hard to say if this is what's causing your problem, or if something else is, though. Does mysite.com/about.php also give you an error?

Related

htaccess not passing parameter to get in php

I have a simple .htaccess file which is supposed to redirect users from /listing/$id to /listing?id=$id. I've tried and tested this script on another domain and it works, yet for some reason it does not work here. I had to alter a couple of things in the scripts, but the logic is almost exactly the same to the original. Maybe I've missed something, but here is the final .htaccess file:
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On
# remove php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
# only allow rewriting to paths that don't exist
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) - [PT,L]
# /listing/$id
RewriteRule ^listing/([\w-]+)/?$ listing.php?id=$1 [L,QSA,NC]
And then when I go ahead and try and retrieve the data on my server side, an empty array is returned. listing.php:
// will return array(0){ }
die(var_dump($_GET));
All help is appreciated, cheers.
I am able to replicate the problem and found it is because of the # remove php extension condition and rule. If I delete those 2 lines then it works as expected. It seems to me as though that rule is counter to the comment above it, i.e. it looks like the rule is adding the php extension not removing it as the comment suggests.
It seems as though REQUEST_FILENAME is being set to "listing" from the url, so if the php file is called listing.php then it will match the first condition but the rule can't be applied because the pattern doesn't then match.
If I keep all your conditions and rules as they are except change the php filename to "listingTEST.php" and the last rule points to that file then it also works because in this case the first condition is no longer met. e.g.
RewriteRule ^listing/([\w-]+)/?$ listingTEST.php?id=$1 [L,QSA,NC]

Set .htaccess rules to specify different folders

I want to set a rule in .htaccess if I enter in the url www.mydomain.com/compare.php set 'public_html' as root otherwise anything come in the url set root as 'public' folder.
RewriteRule ^(?!compare-source.php).*)$ public/$1 [L]
I want to achieve following result.
if url is www.mydomain.com/compare.php hit following file.
public_html/compare.php
if urls are www.mydomain.com/ OR www.mydomain.com/home etc hit following file.
public_html/public/index.php
I am weak in regex and in these apache rules always :-( can someone give me the solution with good description?
Your answers are welcome, please can you describe how this crazy things work in detail. Thanks.
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^((?!compare\.php).*)$ /public/$1 [L]
The RewriteEngine directive enables or disables the runtime rewriting engine.
The RewriteCond directive defines a rule condition. The following Rule is only used if this condition is met; In our case, if REQUEST_URI (the path component of the requested URL) does not (because of !) begin (because of ^) with /public. We need this condition because we don't want to rewrite already rewritten URL - that would cause loop and Internal error 500.
Finally, the RewriteRule will match regex Pattern (^((?!compare\.php).*)$) against part of the URL after the hostname and port, and without the leading slash. If the pattern is matched, the Substitution (public/$1) will replace the original URL-path.
In plain language, if URL path does not begin with compare.php (because of ?!), pick everything (.*) between beginning (^) and end ($) and place it in variable $1. Then replace the original URL path with /public/$1.
#Anubhava's answer is also correct, he just placed both conditions in RewriteRule, and also it could be written even more readable as:
RewriteCond %{REQUEST_URI} !^/public
RewriteCond %{REQUEST_URI} !^/compare\.php
RewriteRule ^(.*)$ /public/$1 [L]
You can use this .htaccess in site root:
RewriteEngine On
# route /home/ or /home to /
RewriteRule ^home/?$ / [L,NC]
# if not compare-source.php or public/* then route to /public/*
RewriteRule ^(?!public/|compare-source\.php$).*)$ public/$1 [L,NC]

.htaccess rewrite rule causes wrong links

I try to make pretty URLs for my website like there:
http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
My .htaccess file looks like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^profile/(\d+)*$ ./profile.php?id=$1
It works, root/profile/123 opens root/profile.php?id=123, but the folder paths are now broken:
root/profile.php?id=123 references for example root/css/style.css
but root/profile/123 wants to reference root/profile/css/style.css, which doesn't exist.
I want to have it both ways working: root/profile.php?id=123 and root/profile/123
How can i fix the link problem?
You could add an alias for anything that would be accessed via /root/profile/ with Alias:
Alias /root/profile /real/path/root
See http://httpd.apache.org/docs/2.4/mod/mod_alias.html#alias
If you are limited to a .htaccess file you can add another rewrite rule before the one you already have:
RewriteRule ^root/profile/(.*)$ /profile/$1 [L]
RewriteRule ^profile/(\d+)*$ /profile.php?id=$1
As the first rule would redirect any access to files at root/profile to profile, which is in turn evaluated by the second rewriterule, this would lead to an infinite loop. The flag [L], which stands for "last" tells the rewriteengine to stop further processing, if it encounters a path starting with "root/profile".

htaccess Redirect Causes Errors

I'm working on a website that has been built sloppily.
The website is filled with regular links that are translated into the corresponding .php pages by the .htaccess page.
This is it:
RewriteEngine on
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
First of all, I would love some help regarding whether or not this page has everything it should. I've never messed with it before.
Secondly and my main issue, if, for example, I would write the address www.thewebsite.com/foobar.html, it would be translated into www.thewebsite.com/cat.php?cat=foobar by the .htaccess page, and it would give a database error (and reveal information about the database).
I've put a check into cat.php which checks if the category exists, but I can't redirect the user to the 404 error page. There's a page called 404.shtml in the website, but redirecting the user to it causes the .htaccess to just change it again to cat.php?cat=404.
Is the way they used the .htaccess page normal? Should I change this system?
And how are users sent to error pages? From what I understood the server should be doing it on its own?
I would love some clarification... There is some much about this subject I don't understand.
Update:
This is my new .htaccess page
RewriteEngine on
RewriteRule ^error.php?err=(.*)$ Error$1.html
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
Because the redirecting is in the code and the user cannot see it, I allowed myself to write the link in a non-clean way. I tried turning it into a clean URL but the following does not do anything:
RewriteRule ^error.php?err=(.*)$ Error$1.html
Can someone please help me understand why? I thought since error.php is a real page, I should put it before the conditional but it didn't work. BTW, I saw in an article about .htaccess that the page should start with Options +FollowSymLinks. It seems to me that everyone sort of has their own way of writing it. Is there a guide or something like that, which I can be sure is authentic and covers all the bases there is about .htaccess?
Thank you so much!!
Using rewrite rules to work around links to .html pages that don't exist is unusual in my experience, but it's really just a different take on "pretty" URLs, e.g. www.thewebsite.com/foobar/ gets routed to cat.php?cat=foobar on the backend.
Your 404 issue is different. You need to be able to display error pages.
One option here is to rewrite requests as long as they don't request an existing file. This is very common for serving up static content like images, CSS files, and the like. To do this, you can use the -d and -f options to RewriteCond, which apply when requesting a directory and file respectively:
RewriteEngine On
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
Now, requests to 404.shtml should go through, because you're requesting an existing file on the filesystem.
Note that the RewriteConds only apply to the single RewriteRule that immediately follows. For additional RewriteRules, also include additional RewriteConds.
Your regex is wrong anywhere. Literal dot needs to be escaped using otherwise it will match any character. Also it is better to use L and QSA flags to end each rule properly.
RewriteEngine on
RewriteBase /
RewriteRule ^koral/([^/]+)/?$ page.php?name=$1 [L,QSA]
RewriteRule ^([^.]+)\.html/([^/]+)/([^/]+)/([^/]*)/?$ cat.php?cat=$1&page=$2&order=$3&dir=$4 [L,QSA]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
RewriteRule ^([^/]+)/([^.]+)\.html$ product.php?cat=$1&product=$2 [L,QSA]

.htaccess file rewrite rule check

I want to rewrite my apache mod_rewrite pattern. I use to do with following:
RewriteEngine on
RewriteRule /^([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
With the above rule I can't get what I expecting.
Can anyone tell what is wrong with my code and how to solve it.
Edited.
my requirement is simple.
I want to redirect if i enter /1.html to /search.php?search_id=1
Thats it.
This should work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !search\.php
RewriteCond %{REQUEST_URI} ^/([0-9]+)\.html [NC]
RewriteRule .* search.php?search_id=%1 [L,QSA]
Maps silently
http://example.com/FileID.html
To:
http://example.com/search.php?search_id=FileID
search.php is considered a fixed string, while FileID can be any number.
For permanent redirection, replace [L,QSA] with [R=301,L,QSA]
You have a typo in your rule try this:
RewriteEngine on
RewriteRule ^/([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
By the way you could also try to use this:
RewriteRule ^/?([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
This makes the leading slash optional.
Are you typing 1.html after the domain? That is if your domain is example.com, then you type example.com/1.html?
If it is you are doing then don't use / before the regular expression in .htaccess file.
Try adding the following into your main .htaccess file. That is located in the directory which has your home page of the website:
RewriteEngine On
#RewriteBase /
RewriteRule ^([A-Za-z0-9]+)\.html/?$ /search.php?search_id=$1 [NC, L]
I have used RewriteBase as a comment. If you use a shared server then keep it as a comment or delete the line.
EDIT: [NC, L] use to tell that, NC flag for rule no need to be case sensitive (no case) and L flag for stop if this was matched.

Categories