How to set index.php as default page? - php

I think after I put the code to remove the .php from URL, the index.php is not working anymore. Here is my .htaccess.
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So, when I try to access some URL like: example.com/x/ does not work, but example.com/x/index/ works.

I don't know if this will help you, but try to use this as reference to your configuration. This has been taken from server that hosts my wordpress page (.htaccess file).
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Try this:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^\.]+)$ $1/index.php [NC,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)/$ $1.php [NC,L]

Related

.htaccess rewrite options for index.php and GET variables

I have a problem with the Rewrite options and I can't find the right answers on the internet so I hope this community can help me.
I am working local on a web project and use XAMPP as my local webserver.
I made a VirtulHost (basic.localhost) and now there is my problem with the mod_rewrite options.
When there is an url: basic.localhost/index.php/var1=option1&var2=option2&var3=option3&var4=option4
I want to rewrite the url to this: basic.localhost/option/option2/option3/option4
The var1 to var4 variables are used as GET variables to select content from an database.
This is my .htaccess now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
And all Rewrite Rules that I tried does not work...
I found an answer for the problem with the variables:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2&var3=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2 [L,QSA]
RewriteRule ^([^/]+)$ index.php?var1=$1 [L,QSA]
</IfModule>
Now there is only the Problem with the index.php.
I finally got a solution for this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2&var3=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2 [L,QSA]
RewriteRule ^([^/]+)$ index.php?var1=$1 [L,QSA]
</IfModule>

Htaccess rewrite rule causing Forbidden access

I'm trying to hide subfolders from url like this:
localhost/imo/public_html/public/ajuda.php to localhost/imo/ajuda
Its working well if the file exist, if not i get a forbidden access like this:
You don't have permission to access
/imo/public_html/public/public_html/public/public_html...public/contacto.php.php.php.php.php.php.php.php.php.php
on this server.
this is my htaccess file:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^$ public_html/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ public_html/public/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA,NC]
</IfModule>
I already search on stackoverflow and it seems that the rules is causing a loop but the solutions provided didn't work on my case so far. Any help at all would be very aprecciated.
Thank you.
UPDATE
Thanks to #Matthijs Otterloo this is the working htaccess script:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^$ public_html/public/
RewriteCond %{REQUEST_URI} !^/public_html/public
RewriteRule ^/?([^/]+)$ /public_html/public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA,NC]
</IfModule>
Try something like this in your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/public
RewriteRule ^/?([^/]+)$ /public_html/public/$1 [L]
Best way would be to set-up propper virtual hosts but this should fix it.

Removing file extensions in .htaccess with wordpress

I have a static website running in conjunction with wordpress in the same dir, it all works fine. However, on the static pages that don't use wordpress I cannot seem to remove the .php file extensions my .htaccess file looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
ErrorDocument 404 /index2.php
RewriteEngine On
RewriteBase /index.php/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
#Alternate default index page
DirectoryIndex index2.php
ErrorDocument 404 http://example.com/index2.php
I have tried adding this code as you usually would but it wont work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try this
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Rewrite url to site.php as site

Here is my files
index.php
blog.php
about.php
I am accessing it as
localhost/index.php or localhost
localhost/blog.php
localhost/about.php
How can i rewrite .htaccess so that i can access it by
localhost/
localhost/blog
localhost/about
Update
My image are not displaying once i applied the below provided .htaccess, can any give fix this ?
Simple try this in .htaccess file :
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.html
Options -MultiViews # optional Depends on Server
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
EDIT:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Disable php execution directly with .htaccess

I have this on my htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
</IfModule>
And in the index.php I get the REQUEST_URI so I can know what page to display.
But I have another files .php, how to make that all php execution like http://domain.com/file.php directs to index.php?
If I the line "RewriteCond %{REQUEST_FILENAME} !-f" then the sitemap.xml is not working.
Sorry for my bad english, and thanks for your help!
You can have it like this:
<IfModule mod_rewrite.c>
RewriteEngine On
# route all *.php to /index.php
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[/\s?] [NC]
RewriteRule !^index\.php$ index.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
</IfModule>

Categories