htaccess: add php extension, except when there is a trailing slash - .htaccess

I understand that to hide a website extension, I can use the following regular expression in htaccess.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This will rewrite, e.g., example.com\post to example.com\post.php.
However, how do I modify the script such that it DOES NOT add .php when there is a trailing slash at the end? This is so that it will recognise the index page.
Currently, if I have example.com\folder\, it will rewrite it as example.com\folder\.php.
Thanks.

Instead of explicitly checking for a trailing slash the usual approach is to check if the request actually references a directory in the server side file system. That can be implemented like this, which also checks if such a php file actually does exist:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?(.+)/?$ /$1.php [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Use another RewriteCond to check the URI for a trailing slash:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can find a working example of this here:
https://htaccess.madewithlove.be?share=d1fa1042-e671-5b7d-ba96-572510689e8b

Related

Advence .htaccess redirect with one exception

I need help with .htaccess redirect.
There is a lot of simmilar answers, but did not find the one for my case.
I ve got this config:
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite to index.php if not file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Now I need redirection for all url but not for 'news_list'
This work for all redirections:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ https://www.newsite.com/$1 [R=301,L]
</IfModule>
For exception I should use:
RewriteRule !^news_list($|/) https://www.newsite.com/$1 [R=301,L]
This works - 'news_list' do not redirect, but problem is, that than '/news_list' doesn't work (because it need point to 'index.php?url=news_list'.
So I need kinda mix of all configs above, but no luck.
Thank you in advance!
This probably is what you are looking for:
RewriteEngine On
# Rewrite everything to new host except for /news_list...
RewriteCond %{REQUEST_URI} !^/news_list(?:$|/)
RewriteRule ^ https://www.newsite.com/$1 [R=301,END]
# Rewrite to index.php if not file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ /index.php?url=$1 [QSA,END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

How to add file exception in mod_rewrite

I'm trying to except my execute.php from the rewrite down below, the code isn't mine and I don't know any apache, how can I do this?
# BASE REWRITE
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-l
RewriteRule ^(.*)$ index.php?data=$1 [QSA,L]
# (Line to ignore execute.php)
</IfModule>
Let's just answer this, since you are new to this place :-)
For future questions: please read a bit about what and how to ask here, what is expected from you, OK?
Instead of a specific exception rule it is easier to add a condition to your existing rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-l
RewriteCond %{REQUEST_URI} !/execute\.php$
RewriteRule ^/?(.*)/?$ index.php?data=$1 [QSA,END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Rewriting .htaccess to remove sub-directory from URL

I want to mask a folder in URL, so instead of www.mysite.com /employers/university-of-worcester/profile.html there would be www.mysite.com /university-of-worcester/profile.html , meaning that sub-directory "employers" is hidden.
There will be many folders created inside "employers" folder.
This is a matter of modifying .htaccess, I have tried a lot of solutions that I have found on stack.
The latest line of code I tried to add is:
RewriteRule ^employers/(.*)$ /$1 [L]
My .htaccess looks like this now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^employers/(.*)$ /$1 [L]
</IfModule>
If I go to URL www.mysite.com/employers/ this will redirect to www.mysite.com/ (the homepage), which I am happy about, but if I try www.mysite.com/university-of-worcester/profile.html I get "The page can’t be found.", but the home profile.html is definitly inside "university-of-worcester" folder.
I am using wordpress.
This probably is what you are looking for:
RewriteEngine on
RewriteRule ^/employers/?(.*)$ /$1 [R=301,QSA]
RewriteCond %{REQUEST_URI} !^/employers/
RewriteRule ^/?(.*)$ /employers/$1 [END,QSA]
This will redirect direct requests to that folder and internally rewrite requests to that folder.
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

htaccess links with query strings not opening on pc,

my htaccess file looks like this:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.php$ sitemap.xml [L]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
I want my links look like:
https://kaznews.kz/news/477800
as they are at the time,
but when I have QUERY STRING they not opening links like: https://kaznews.kz/news/477800?google
I want either delete the ?mark and the query, or add them at the end but show the correct page.
RewriteRule ^(.*)$ /index.php?/$1 [r=301,L,QSA]
this is not suitable for me because it gives me
such result: https://kaznews.kz/index.php?/news/477800&google with index.php inside, but there will be duplicate links then.
Well, looks like that is roughly what you are looking for:
RewriteRule ^/?news/(\d+)/(.+)$ /news/$1?$2 [END,QSD]
That rule will internally rewrite requests to /news/477800/google to /news/477800?google.
Update:
From your comments below we learned that what you actually appear to ask is how you can remove, so ignore any query arguments specified in the request. Though this is a questionable thing to do (as reasoned in the comments) here is a rule to achieve that:
RewriteRule ^/?news/(\d+)$ /news/$1 [END,QSD]
General:
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

.htaccess permanent redirect with query string

I want to have a permanent redirect where whoever visits the old url is automatically sent to the new one:
From: https://example.io/users?username=value
To: https://example.io/value
The new URL already works, but I just need the redirect. I already have this code:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ users.php?username=$1 [QSA,L]
I'd say this should roughly be what your question asks for:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^username=(.+)$
RewriteRule ^/?users\.php/?$ /%1 [R=301]
Those lines should work likewise in the http servers host configuration or in dynamic configuration files (".htaccess").
It redirects all incoming requests to the old URL "/users.php?username=value. I fail to see how the code you posted should help here, which is why I wrote above rules from scratch instead of modifying yours.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Resources