The title explains the basics, now, here comes the problem:
I have many files that look like this:
<?php include 'mynavbar.php';
include 'myheader.php';
include 'myfirstline.php';
include 'mybuttons.php';
include 'myadvert.php';
include 'myimg.php';?>
And I can't edit all the files and remove the .php. How can I not show the .php in the address bar but still be able to include the pages by using the .php ending
Following code should work for you in .htaccess file to hide .php extension:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# to make `/path/index.php` to /path/
RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.php [NC]
RewriteRule . %1 [NE,R=301,L]
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Also remember that these rules will have NO impact in your php includes eg: include 'myheader.php';
It is because those includes are processed by php itself and don't go through Apache.
The following RewriteRules will assume the PHP extension for any file that is not otherwise matched. It should go at the bottom of your RewriteRules in your .htaccess file.
# Force PHP extension if not a directory
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^(.*)$ - [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^((.*/)*[^./]+)/*$ $1.php [L]
This is for URL Rewriting. As far as include statements, you still need the PHP extension as these are physical file paths. If you need to so something fancy there, you should look into symbolic links.
You are not taking the correct path for fixing the problem you trying to solve.
If I understood well, what you want to do is having an url like :
http://example.com/my/url
instead of (for example):
http://example.com/my/url.php
If this is the case, what you should look for is rewrite url
Related
I currently have a CRUD that has the following file structure:
The index.php file redirects to the add-product.php, delete.php and other files. The problem is, the URL looks like: myapp.com/resources/views/add-product but I want it to look like myapp.com/add-product. I found other StackOverflow posts similar to this, but it redirects to the desired link, instead of redirecting to the correct link but showing the desired one, and because of that the site doesn't work ("The requested URL was not found on this server"). The .htaccess file looks like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^resources/(.*)$ /$1 [L,NC,R]
I'm also using the file to be able to remove the .php from the URLs. The last line is the one handling the wrong redirection. How can I go about this? Thank you for your time.
You may use it like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /myapp/
# external redirect to remove /resources/views/ from URLs
RewriteRule ^resources/views/(.+)\.php$ $1 [L,NC,R=302]
# internal rewrite to add /resources/views/ and .php to show content
RewriteCond %{DOCUMENT_ROOT}/myapp/resources/views/$1.php -f
RewriteRule ^(.+?)/?$ resources/views/$1.php [END]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
I have a web project with two directories: "core" and "public".
"Core" contains all of the controllers, views, and files required for the Model-View-Controller.
"Public" contains all public files, like js, css, and less, that can be accessed directly.
I have the following .htaccess in the main directory:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js|\.less|\.coffee)$
RewriteRule ^$ core/ [L]
RewriteRule (.*) core/$1 [L]
</IfModule>
However, it still rewrites those files to the 'core' directory.
What am I doing wrong?
I recommend you use this instead:
RewriteEngine on
# First, check if a specific type is being requested
RewriteCond %{REQUEST_URI} !\.(png|jpg|gif|jpeg|bmp|css|js|less|coffee)$ [NC]
# Second, check if the request is for an existing file
RewriteCond %{REQUEST_FILENAME} -f
# If both conditions are true, then skip rewriting
RewriteRule ^ - [L]
# Otherwise, continue:
RewriteRule ^$ core/ [L]
RewriteRule (.+) core/$1 [L]
The reason your assets were being sent to core is because the conditions only work for the first rule, which was for your application index. Using this method tells mod_rewrite to skip rewriting if an asset is being requested. Once it does that, it can continue with all other rules.
I have a file at www.domain.com/blog/blog.html that I want to rewrite as www.domain.com/blog.
I have figured out the rewrite for removing the extension .html but am having either circular issues or end up pointing the blog rewrite to the blog folder when trying to do the above.
I am new to rewrite syntax, so help would be much appreciated!
Create /blog/.htaccess if it doesn't already exist and place this rule:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^/?$ blog.html [L]
# To internally forward /blog/file to /blog/file.html
RewriteCond %{DOCUMENT_ROOT}/blog/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
I believe it might be a possible duplicate. But I tried my best to search for such a thing that will suit my needs and I found, none.
So here's basically what I have so far, and I will explain what I need modified.
# Forbidden Access
ErrorDocument 403 /403.php
# Not Found
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
</IfModule>
<IfModule mod_rewrite.c>
# Strip off .php extension if it exists
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /403.php$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
</IfModule>
Now this seems to be working flawlessly. But it has one error. Let me explain first.
1) It does automatically strip-off .php extension if it exists. Not sure if it strip off .php if it is url of an external request. Forgot to check, but maybe you already know so you can tell me ?
2) When I type this... "http://website.dev/img/" it does give me an "403 Forbidden Access". So that's all good.
3) When I try this... "http://website.dev/index" it does load the page even if there is .php extension manually added it will strip it off. So All good in here too...
4) When I try random path like this... "http://website.dev/asdasd" it does give me an "404 Not Found". So we're good in here as well.
But the main problem is here...
5) When I try following... "http://website.dev/dashboard/index" it give me an 404 Not Found even tho it should be loading without issues. It appears for all pages within dashboard directory.
Can you help me to modify that htaccess above please ? I am really tired of searching and I don't know regex at all.
That is because of the faulty regex used in your very last rule to silently add .php extension. Change last rule to:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
Here's my translation of you rules:
# Strip off .php extension if it exists
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
Bad comment. You regexp means: strip off all files that have 3 uppercase first and and dot php in it. Maybe you've forgotten the ending $?
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /403.php$1 [R=301,L]
Why is that? Just do a redirect, and Apache will handle the 301 it for you:
RewriteRule .* - [L,R=403]
And then last question: why you strip off .php extension, if you re-add it later on? (°_o)
So here's what you should do, with some examples, and adapt them you fit your needs:
First test if the file has no special treatment. If so, stop immediately, like this:
RewriteRule ^/(robots\.txt|404\.php|403\.php)$ -
Then test if someone is trying to hack. If so, redirect to whatever you want:
RewriteRule (.*)test.php - [QSA,L]
RewriteRule (.*)setup.php http://noobs.land.com/ [NC,R,L]
RewriteRule (.*)admin(.*) http://noobs.land.com/ [NC,R,L]
RewriteRule (.*)trackback(.*) http://noobs.land.com/ [NC,R,L]
Then, only after this, forbid the php extension:
RewriteRule (.*)php$ - [L,R=404]
Then, accept all static "known" file extension, and stop if it matches:
RewriteRule (.*)(\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico|mpg|mp3|ogg|wav|otf|eot|svg|ttf|woff)){1}$ $1$2 [QSA,L]
Now you can do some testing. If the URI ends with a 'aabb/', test if you have a file named aabb.php, and if so, go for it:
RewriteCond %{REQUEST_URI} (\/([^\/]+))\/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule (.*) %{DOCUMENT_ROOT}/%1.php [QSA,L]
If nothing is handled, and you get here, it's a problem, so stop it:
RewriteRule .* - [L,R=404]
FYI, all those sample rules are deeply tested on a production server.
And now with that, you have all what you need to do something good & working.
Hoping someone can help. I use nibbler to check out my website to make sure it behaves well and one of the categories is URL FORMAT which
Avoid use of file extensions wherever possible.... Consider URL rewriting as an effective
and transparent means of creating appropriate URLs.
Anyway, I use Dreamweaver to create and edit the site and if I convert it to using index.htm files in directories (which would solve this) then this just seems to make life complicated.
Is there some MAGIC that I can do in the .htaccess file that means i can still upload it as .htm's and it does some cool stuff in the background and shows them better.
for example I would like
http://www.beingchildren.org/Children-Charity-Blog.htm
to become
http://www.beingchildren.org/Children-Charity-Blog
Can anyone help?
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.htm[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.htm -f
RewriteRule ^(.+?)/?$ /$1.htm [L]