Can't access website's root page - php

Usually, going to my website, qpcftw.cu.cc would load the page qpcftw.cu.cc/index.php transparently, without the /index.php part showing in the url. I've been trying to get rid of the .php extensions using .htaccess. This is the contents of my .htaccess file:
<Files />
ForceType application/x-httpd-php
</Files>
RewriteEngine on
RewriteBase /
RewriteRule ^.htaccess$ - [F]
RewriteRule ^([A-z,0-9,_,-]+)?$ $1.php [QSA]
RewriteRule ^([A-z,0-9,_,-]+)/index\.html$ $1.php [QSA]
ErrorDocument 403 php/error.php
ErrorDocument 404 /404.php
ErrorDocument 405 php/error.php
ErrorDocument 408 php/error.php
ErrorDocument 500 php/error.php
ErrorDocument 502 php/error.php
ErrorDocument 504 php/error.php
I need to meet 3 criteria:
qpcftw.cu.cc/index.php == qpcftw.cu.cc/index
Visiting qpcftw.cu.cc/forum/ still loads my PHPBB forum at qpcftw.cu.cc/forum/index.php
qpcftw.cu.cc loads the index.php file transparently, without it shown in the URL
So far my current .htaccess fulfills the first 2 needs, but breaks the 3rd. Help! :/

The following .htaccess code automatically does all the extension hiding magic:
Options +MultiViews

Instead of blindly adding .php to everything, check if the .php file exists first
RewriteCond $1.php -f
RewriteRule ^([A-z0-9_-]+)?$ $1.php [L,QSA]
RewriteCond $1.php -f
RewriteRule ^([A-z0-9_-]+)/index\.html$ $1.php [L,QSA]
This still works for 1 and 2, and 3 now also works because the code will test if index.php.php exists (the request is for index.php), which it doesn't (I hope!), so the RewriteRule will fail.
I've also removed the commas from the regex classes (you can't use those to indicate several groups, putting it in there just means commas are allowed in URLs), and added the [L]ast flag to the rules for better performance

Related

htaccess - redirect to error pages issue (with slash or without slash)

There is an issue with redirection to error pages:
example.com/test - will redirect to 404 error page
but
example.com/test/ - will go to the white "File not found." page
to mention:
it was working properly until some time ago (maybe update of PHP version ??)
same behavior with www/http/https version of the links
standard structure of the links is www.example.com/test/
.htaccess file code
<Files .htaccess>
order allow,deny
deny from all
</Files>
RewriteEngine On
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteRule sample/(.*)/(.*)/$ /sample.php?$1=$2
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 410 /410.php
The difference with your URLs that end in a trailing slash is that they are unconditionally rewritten to the corresponding .php file. The URLs that do not end in a trailing slash are not rewritten - nothing happens.
You are seeing the same basic "File not found" response when you directly request a non-existent .php file, regardless of whether the request is rewritten (by your rules) or not.
The "problem" might be due to the way PHP is implemented on your server. For instance, if all *.php requests are proxied to an alternative backend process then this is going to bypass your .htaccess file on the application server and the "basic" 404 response you are seeing is possibly coming from the proxy, not your application server.
You may be able to resolve this by first checking that the .php exists before rewriting to it (so it doesn't trigger a 404). And if none of your URLs contain a .php extension, you could also force any direct request for .php files to 404 (on your server, before the request is proxied - if that is what's happening).
RewriteEngine On
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteRule sample/(.*)/(.*)/$ /sample.php?$1=$2
The first two rules can also be combined into one. You are missing L flags on all your rules. You need to ensure that MultiViews is disabled, otherwise the last rule will not work.
Also, the regex in the last rule needs to be anchored and made more specific since it is matching too much, eg. /sample/foo/bar/baz/qux will be rewritten to /sample.php?foo/bar/baz=qux, which I assume is not the intention.
Try the following instead:
Options -MultiViews
RewriteEngine On
# Force any direct request for ".php" files to 404
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.php$ - [R=404]
# Rewrite to ".php" file - 1 or 2 path segments
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+(/[^/]+)?)/$ $1.php [L]
# Rewrite "/sample/one/two/"
RewriteRule ^sample/([^/]+)/([^/]+)/$ sample.php?$1=$2 [L]
Reference:
Another recent question that has a very similar issue and was resolved in the same way:
Custom 404 error handler in htaccess not working for non-existent ".php" files
The problem is with ending slash of RewriteRule ^([^/]+)/$ $1.php
If you write RewriteRule ^([^/]+)/?$ $1.php the tailing slash would be optional.
edit
You should also add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
before RewriteRule statements, because of server loop - when the file exists the statements will break loop by skipping rewriting.

Htaccess RewriteRule redirects to 404 page

My basic requirement is:
Remove .php extensions from all the urls.
Rewrite the urls from http://localhost/unsync/softwares/page_name/sub_category/ to http://localhost/unsync/softwares.php?p=page_name&sub_cat=sub_category
The following is my .htaccess code:
# Do not remove this line, otherwise mod_rewrite rules will stop working
Options +MultiViews
RewriteEngine On
RewriteBase /
#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
#Prevent directory listings
Options All -Indexes
#Error Documents
ErrorDocument 400 /unsync/error.php?code=400
ErrorDocument 401 /unsync/error.php?code=401
ErrorDocument 402 /unsync/error.php?code=402
ErrorDocument 403 /unsync/error.php?code=403
ErrorDocument 404 /unsync/error.php?code=404
ErrorDocument 500 /unsync/error.php?code=500
ErrorDocument 503 /unsync/error.php?code=503
#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
RewriteRule softwares/(.*)/(.*)/$ /softwares.php?p=$1&sub_cat=$2 [L]
DirectoryIndex index.php
The problem I am facing is that, RewriteRule fails. I mean, when I try to access softwares/page_name/sub_category, its throwing a 404 error.
Note: Its removing the .php extensions properly and working fine with normal pages.
The problem is that your rewrite rule rewrites all requests to /unsync/request.php, you have to check the existance of php file before rewriting the request,
#Remove extensions
RewriteCond %{DOCUMENT_ROOT}/unsync/$1.php -f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
Or you can simply exclude the slash in pattern so that it can not conflict with your other rule
RewriteRule ^([^\/.]+)$ /unsync/$1.php [NC,L]
Rewrite rules are tried in order.
This means softwares/page_name/sub_category is rewritten by the first rule to /unsync/softwares/page_name/sub_category.php, which is not found. Therefore, you get a 404 Not found error.
After a long day of research, I could finally resolve my issue on my own as follow (if anyone facing similar issue is searching for solution):
#If both p and sub_cat are issued
RewriteRule ^softwares/(.+)/(.*)$ /unsync/softwares.php?p=$1&sub_cat=$2 [NC,L]
#If only p is issued
RewriteRule ^softwares/(.*)$ /unsync/softwares.php?p=$1 [NC,L]
#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
DirectoryIndex index.php

Finding the php file which is executed (the name is hidden by .htaccess)

I've a very weird situation on my own website.
When I try to execute this page:
http://www.****.com/Militari/Guida-scelta-.htm
Or this page
http://www.****.com/Militari/Guida-.htm
I get the same result. Looking to the .htaccess/PHP files it's a completely unpredictable result.
It looks like if the executed php file would make a DB search and try to find the best matching result.
This is on a shared hosting.
How can I find which PHP file is being executed?
The .htaccess
RewriteEngine on
RewriteRule ^Cinema/Interviste/([^/]*)\.htm$ /Cinema/Interviste/film.php?title=$1
RewriteRule ^Cinema/dvd/([^/]*)\.htm$ /Cinema/dvd/film.php?title=$1
RewriteRule ^Cinema/([^/]*)\.htm$ /Cinema/film.php?title=$1
RewriteRule ^Attori/(.*)\.htm$ Attore.php?name=$1
RewriteRule ^Registi/(.*)\.htm$ Regista.php?regia=$1
RewriteRule ^(.*)\.htm $1\.php
RewriteRule filmrecensiti\.xml filmrecensiti\.xml\.php
RewriteRule filmrecensiti2\.xml filmrecensiti2_old\.xml
RewriteRule ^Elenco\sFilm\s(.)\.php Elenco_Film_Recensiti\.php?starting_letter=$1
RewriteRule ^Elenco\sFilm\sRecensiti(.*) Elenco_Film_Recensiti$1
RewriteRule ^(.*)\.js php_js\.php\?id=$1\.js
RewriteRule ^(.*)\.css php_css\.php\?id=$1\.css
#RewriteRule ^feeds/(.+)$ feeds_items/$1 [L]
ErrorDocument 400 /not_found.htm
ErrorDocument 401 /not_found.htm
ErrorDocument 403 /not_found.htm
ErrorDocument 404 /not_found.htm
ErrorDocument 500 /not_found.htm
EDIT
I had missed the .htaccess in the subfolder
its content is
RewriteRule ^([^/]*)\.htm$ film.php?title=$1
I found the problem thanks to your advice.
This helped a lot: $_SERVER['PHP_SELF'] or $_SERVER['SCRIPT_NAME']
From what you've posted, the only applicable rule would be RewriteRule ^(.*)\.htm $1\.php suggesting that http://www.****.com/Militari/Guida-scelta-.htm would actually serve up http://www.****.com/Militari/Guida-scelta-.php.

mod_rewrite issue with parameters

I have the following .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^portfolio-single/([^/.]+)/?$ portfolio-single.php?id=$1 [L]
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
order deny,allow
</Files>
Options -Indexes
ErrorDocument 400 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 405 /error.php
ErrorDocument 408 /error.php
ErrorDocument 500 /error.php
ErrorDocument 502 /error.php
ErrorDocument 504 /error.php
Currently all pages that end with the php extension are working fine. So for example www.somewebsite.com/blog.php is now accessable via /blog. However I want a certain page, namely, portfolio-single.php?id=123 to be accessible via /portfolio-single/123. The 4th line you can see code that I took from this website, and I have been trying to get it to work but keep getting a 404 when I access portfolio-single via /portfolio-single/123. What am I missing? A follow up question is if I have to reboot XAMPP for changes to the .htaccess file to take effect (so far it has worked without).
Try swapping the two rules. The last rule is more specific so it should match first.
While your current approach works, it's more common to use one catch-all rule and a generic handle script which does the routing.
RewriteRule ^(.*)$ handler.php?path=$1 [L]
Then handler.php can decide what to do with blog and portfolio-single/x and non-existing-page.
The router can now be implemented in PHP which gives you more freedom, like dynamically adding routes and matches based on more than a regexp.
Another advantage of this approach is that your application now has a single point of entry. This makes bootstrapping much easier since there is only one path.
For adding .php extension you must check whether corresponding .php file exists firts:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [NC,L]
RewriteRule ^portfolio-single/([^/.]+)/?$ portfolio-single.php?id=$1 [L,NC,QSA]

.htaccess error after change server

I have changed my hosting and get a 404 error on one website where we have using .htaccess file and code like this, now what happening sub-cat page showing 404 error http://www.natural-stones-india.com/sandstone/Golden%20Brown%20Black.html
.htaccess code is like this ----------- can anyone help?
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^sandstone/(.*).html$ product.php?sandstone=$1
RewriteRule ^slate/(.*).html$ product.php?slate=$1
RewriteRule ^marble/(.*).html$ product.php?marble=$1
RewriteRule ^granite/(.*).html$ product.php?granite=$1
RewriteRule ^artifacts/(.*).html$ product.php?artifacts=$1
RewriteRule ^page/(.*)/sandstone/(.*).html$ product.php?page=$1&sandstone=$2
RewriteRule ^page/(.*)/granite/(.*).html$ product.php?page=$1&granite=$2
RewriteRule ^page/(.*)/marble/(.*).html$ product.php?page=$1&marble=$2
RewriteRule ^page/(.*)/artifacts/(.*).html$ product.php?page=$1&artifacts=$2
RewriteRule ^page/(.*)/slate/(.*).html$ product.php?page=$1&slate=$2
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteRule ^action/(.*)/id/(.*)/cat/(.*)/sub_cat/(.*)shoppingcart.html$ shoppingcart.php?action=$1&id=$2&cat=$3&sub_cat=$4
RewriteCond %{HTTP_HOST} ^natural-stones-india.com
RewriteRule ^(.*)$ http://www.natural-stones-india.com/$1 [R=301,L]
<Files .htaccess>
order allow,deny
deny from all
</Files>
ErrorDocument 404 /missing.html
OK, let's analyze some facts
Webserver does not respond with 500 Internal server error)
Conclusion: Your .htaccess file seems to be syntactically correct
Webserver does some of the rewrites correctly, also the custom error document is working
Conclusion: Your .htaccess file is actually read by the webserver
By the first look all rewrites based in the document root seem to work
http://www.natural-stones-india.com/granite.html works, while http://www.natural-stones-india.com/granite/Pink%20Granite.html does not
Conclusion: You have a problem with relative paths
Possible solution:
RewriteEngine On
RewriteBase /
...
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#RewriteBase
Or set the rewrite targets absolute
e.g.
RewriteRule ^sandstone/(.*).html$ /product.php?sandstone=$1
Try changing your access rights to the .htaccess file
You should be able to change these through FTP.
You site seems fine, since this i working: http://www.natural-stones-india.com/product.php?sandstone=Golden%20Brown%20Black

Categories