I am using mod_rewrite to hide file extension this code is working prefect but when i add slash at the end of url it's show error 500 internal server error
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Please help me
but when i add slash at the end of url it's show error 500 internal server error
This is because the slash is being grouped together in the (.*) and then referenced by $1, so a request like:
http://your.host.com/my_file/
is getting rewritten to:
http://your.host.com/my_file/.php
The reason is that the condition %{REQUEST_FILENAME}\.php exists, because the trailing slash gets ignored. You just need to include the match against the trailing slash in your RewriteRule's regex:
RewriteRule ^(.*?)/?$ $1.php
Related
it's really difficult to understand htaccess's work, I have this code:
#Remove slash on the end
RewriteRule ^(.*)/$ $1 [R=301]
#Don't use .php extension in URL
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php
#All the directories should be redirected to index.php
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . index [R=301]
Structure of my root folder: index.php, .htaccess and dir directory with test.php file inside.
I know that this file re-executes multiple times until the changes in URL are over, so if I'm writing mysite.ru/index/dir/ to address bar for example (I use existing index file as directory, otherwise, if I use words that aren't related to existing file names, it's works), I'm expecting 404 page (Slash removes, dir.php doesn't exist, index/dir doesn't exist, file executes again, URL didn't changed and it's over), but I'm getting 500 error and 10 redirects in error.log file. Something wrong with .htacess file, and I can't understang what.
You can use END flag or RewriteCond %{ENV:REDIRECT_STATUS} ^$ condition to prevent the infinite rewrite loop error.
END flag works on Apache version 2.4 or greater
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [END]
The following condition works on all versions
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php
The reason why you got internal server error (Too many redirects) is because your rule rewrites the same request multiple times. Your rule gets triggered when you request /filename/blabla .
Since the /filename/blabla doesn't map to an existing file your condition RewriteCond %{REQUEST_FILENAME}\.php -f only checks the first path segment /filename . the condition is met and the rule gets triggered in loop.
This is my current .htaccess file:
RewriteEngine on
# remove trailing slash
RewriteRule (.*)(/|\\)$ $1 [R]
# everything
RewriteRule ^(.*?)$ /handler.php?url=$1 [L,QSA]
However, this doesn't work, it throws a 500 Internal Server Error
My previous .htaccess file looked like this:
RewriteEngine on
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)(/|\\)$ $1 [R]
# everything
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /handler.php?url=$1 [L,QSA]
And it worked, except for specific files. However, now I'd like the specific files to redirect into the handler as well. Is there a way to use RewriteRules without the RewriteConds?
Without RewriteCond for file check you can tweak your regex like this:
RewriteEngine on
RewriteBase /
# remove trailing slash for non directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R,L]
# every request not for handler.php
RewriteRule ^((?!handler\.php$).*)$ handler.php?url=$1 [L,QSA]
With the help of CBroe and Sumurai8, I was able to fix the problem on my own. The problem was not that you can't have RewriteRules without RewriteConds, but that if you rewrite every url into a single file, it will rewrite requests to that specific file to itself once more, creating an infinite loop.
The new .htaccess:
RewriteEngine on
# remove trailing slash
RewriteRule (.*)(/|\\)$ $1 [R,L]
# everything
RewriteCond %{REQUEST_URI} !^/handler.php$
RewriteRule ^(.*?)$ /handler.php?url=$1 [L,QSA]
Relevant resource: Request exceeded the limit of 10 internal redirects
I'm trying to remove the trailing slash of all urls. Whatever htaccess script lines I try, it always redirects to what seems the full server directory.
example.com/XYZ/ weirdly redirects to example.com/customers/b/7/3/example.com/httpd.www/XYZ – Not found.
Basically, I'm not using any subdirectories but getting data out of a database according to what's the last string after the last slash. So the "Not found" error is ok, because there actually isn't an existing folder.
I'm new to htaccess, so I was just trying out whatever lines I found.
Rewrite so that anything opens index.php and not a folder (which works fine without trailing slash, Engine is on):
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?$1
I failed by using this to remove the slash:
RewriteRule ^(.+)/$ /$1 [R=301,L]
So basically what I'm trying to do is:
Open any url as index.php. For example example.com/XY doesn't actually redirect but show/open index.php (I think I achieved that with above two lines)
While doing so, I'm trying to also remove the trailing slash from e.g. example.com/XY/, so example.com/XY and example.com/XY/ both show/open index.php
Thanks for your wisdom!
You can do:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteRule ^([\w-]+)/?$ /index.php?$1 [L,QSA]
I have the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
RewriteRule ^([^\.]+)/([^\.]+)/$ $1.php?id=$2
I had problems with with the absolute URI, it solved. Now I want to use the last row for the following:
domain.com/query/ping/2/
to
domain.com/query/ping.php?id=2
I think the code is good, but I still get back error 404. Should I give some rewrite conditions? I searched for this code but haven't found anything useful.
Based on where you've placed your htaccess file in your previous question, you probably want something like this:
RewriteCond %{REQUEST_URI} ^/query/([^/]+)/([^/]+)/?
RewriteCond %{DOCUMENT_ROOT}/query/%1.php -f
RewriteRule ^ /query/%1.php?id=%2 [L]
The important issue is that you need to check for /query in the beginning of the URI. What you have, ^([^\.]+)/([^\.]+)/$ won't match the /query part along with the 2 pathnames after it. Your regex only matches 2 pathnames.
The line RewriteCond %{DOCUMENT_ROOT}/query/%1.php -f is similar to the condition you have above where it checks to see if the requested PHP file actually exists, otherwise it won't blindly rewrite. This condition ensures if someone tries to go to:
http://domain.com/query/blahblahblahblah/blah
Your server won't return a 404 error saying /query/blahblahblahblah.php doesn't exist.
What I'm trying to achive is to have all urls on my page look like http://domain.com/page/, no extensions, but a trailing slash. If a user happends to write http://domain.com/page or http://domain.com/page.php it will redirect to the first url. After some googling i found this code, and it's close to working, but when you leave out the trailing slash in your request the url becomes something like http://domain.com/Users/"..."/page/ and therefor returns a 404.
My .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !(.*)/$
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
I've been trying to add an additional rule but I really don't get any of this and I haven't been able to find any answers.
For a scenario like this one, the .htaccess author has to consider both what the browser URL bar should display and what file the web server should return/execute. Note also that each external redirect starts the processing of the rewrite directives over.
With that in mind, start by taking care of which file is returned when the URL is in the correct format:
RewriteEngine on
RewriteRule ^/?$ /index.php [L]
RewriteRule ([^./]+)/$ /$1.php [L]
Then, deal with URLs with no trailing slash by redirecting them with [R=301]:
RewriteRule ^/(.*)\.[^.]*$ http://www.example.com/$1/ [R=301,L]
RewriteRule ^/(.*)$ http://www.example.com/$1/ [R=301,L]
Note that the first of these two rules should also take care of the case where there is a filename (like something.php) but also a trailing slash by eliminating the filename extension and re-adding the slash.
Keep in mind that, if your internal directory structure does not match what the web server is serving (as is often the case in shared hosting scenarios), you will likely need to add a RewriteBase directive immediately after the RewriteEngine directive. See the Apache docs for an explanation.