I have the following rewrite in my .htaccess file which removes the .php extension from files, converting for example so.com/question.php to so.com/question.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
However this also breaks the default DirectoryIndex behaviour, in which just typing the directory will redirect to the index file in the folder, e.g. so.com/answer displays so.com/answer/index.php
Simply combining the above code with DirectoryIndex index.php does not achieve both results.
Can someone help me combine these two functions, or rewrite the code to exclude index.php files, which would achieve the same result?
I'm thinking you just need to verify that the file exists prior to doing the rewrite, that way you'll leave 404 and directoryindex behaviours intact:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
(not tested)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I tested and it is working fine.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]
verify files and folder and also, add RewriteBase /
Related
I have a static html website and I am using the following code to remove the .html extension for SEO reasons:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
Problem is when I add a subdirectory /blog/ I get a 403 Forbidden error. Any help please?
Try this, you were missing the html condition.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I have my htaccess and it already does couple of good things (it hides www and .php in my url) but I also need to add one additional feature to it.
I have the main domain called: wdd.ie
I have a root folder called: wdd.ie
Most important files are kept in the root.
I have also the following file in the root: about.php
I love to access about.php file by calling non existing directory: www.wdd.ie/myweb/
And my full URL path looks like this: www.wdd.ie/myweb/about
I would like to access the file (stored in the root) to allow non existing directory in url but to ignore it
Could anyone help!? Please, please help!
As this new desired htaccess feature complexity is now probably beyond my skills.
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
If I've understood correctly you want the following url www.wdd.ie/myweb/about to offer up content from www.wdd.ie/about.php but keep the url the same?
If so, then the following should work
RewriteRule ^myweb/about$ /about.php [L]
so I want to rewrite a URL. The default URL is http://example.com/m.php?i=random_string
Here's the rule
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
RewriteRule ^m/([^/]+)/?$ m?i=$1 [L]
However, when I access http://example.com/m/random_string, the page comes up but my external css and js files don't load. Why does this happen? I thought that the directory does not change from the original rewritten URL.
The directory indeed does not change, but browser doesn't know that - what it gets looks like a directory, such as m/something, so it tries to load the CSS file relative to this directory.
To fix that, use absolute paths to your CSS and JS in the page, ie.:
/css/style.css
(the initial forward slash makes it absolute)
RewriteEngine On
RewriteRule ^m/([^/]+)/?$ m.php?i=$1 [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)(\?.*|)?$ %{REQUEST_FILENAME}.php$2 [QSA,L]
Have your rules like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^m/([^/]+)/?$ m.php?i=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d and RewriteCond %{REQUEST_FILENAME} !-f will prevent first rewrite rule to be applied for real files and directories.
When I don't edit my .htaccess file, my image path reads something like: http://this.website.com/codeigniter/inc/images/logo.jpg.
However, as soon as I add this code into the .htaccess file:
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php?$1 [L]
(to remove the index.php from the URL), I get 404 error, even with the exact same path.
How do I remove index.php and still have access to my images?
RewriteEngine On
RewriteCond $1 !^index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?$1 [L]
The two new RewriteConditions would exclude existing directories and files (and so images) from redirection.
First of all, I know there are plenty of similar questions about this around, but
None of them seem to work for me
None of them actually address exactly what I want
What I want is, as the title suggests, to redirect URLs without the .php extension to the actual .php file - changing the URL if possible (which I presume is just handled by [R=301]). The latest thing I tried was this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ $1.php [R=301]
That doesn't work. I can still cant access /about.php with /about. (.htaccess rules themselves are working fine though)
I understand RegEx fine, but htaccess rules just mess with my head =[
So what should I do?
Now I know what you're thinking
One of you will say this: "Why do you want to do this? Just get rid of extensions completely and access your pages via /about or /about/ with a trailing slash."
I'd like to do that, it looks quite good. Problem is SEO - from which I assume my page ranks will get annihilated because all of a sudden they're on different URLs. So before you suggest that, suggest how I'd keep my page ranks first.
What I'm actually doing is essentially URL shortening for a poster - it's a lot easier for people to remember mywebsite.com/about than mywebsite.com/about.php.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
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 [NC]
RewriteRule ^ %1 [R=302,L]
# To internally forward /dir/foo/ to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Please Make sure you have MultiViews options disabled using: Options -MultiViews
Beware of Apaches multiviews
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.
Please make sure that there's mod_rewrite on your Apache HTTP Server and try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ /$1.php [R]
But clear your cache or use another browser first before checking the redirecting dynamic URLs, because you've been previously used the [R=301] flag! For more info. about that, please visit: https://stackoverflow.com/a/15999177/2007055
Could you try this one but it's quite the same as the previous code:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ http://%{HTTP_HOST}/$1.php
And when it works, try adding these two conditions above the rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ http://%{HTTP_HOST}/$1.php
And when any of these codes above does not work, I think there's a problem in your Apache HTTP Server.
That works for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
You can chain it if you want e.g.
RewriteEngine On
# Remove trailing slashes.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
# Redirect to HTML
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# Redirect to PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
# Redirect to ASP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.+)$ $1.asp [L,QSA]