Htaccess code redirects to XAMPP index page - php

I am trying to write htaccess codes which redirect all URLs apart from directly linked files (e.g images & css files) to my index.php file for processing
http://localhost/testsite/login
redirects to
http://localhost/testsite/index.php?cmd=login
But the htaccess code instead redirects me to the XAMPP homepage. This is my htaccess code
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?cmd=$1 [QSA,NC,L]
What am I doing wrong?

Remove / from target and use proper RewriteBase:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /testsite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?cmd=$1 [QSA,L]
/index.php will route it to site root index.php instead of index.php in current directory.

Related

how to change default page index.php to url

i have my .htaccess file here..
RewriteEngine On
RewriteBase /faith/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
RewriteBase /faith/ is the main folder on my local... but my problem is when i upload it in the cpanel i dont have any idea on how to use the url as my index.php .. i tried to search for it and it says that i need to change the rewritebase in a url.. but that doesn't seems to work cause what i want is my index.php will be a url.
my login page is index.php
and i want it to become sample.com so whenever i search it the only thing that i need to do is to type sample.com
Just add this line at top of your .htaccess:
DirectoryIndex index.php
This will load index.php by default when you visit http://example.com
your script seems ok in .htaccess file, you just need to use the redirection file name just like you are using index.php , you can use sample.php overthere.
RewriteEngine On
RewriteBase /faith/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ sample.php?uri=$1 [QSA,L]

How to hide folder names and file extension in URL?

I have following requirements which i believe can be accomplished using .htaccess file.
Requirements-
Hide Folder names and file extension in URL Eg. www.example.com/subfolder/subfolder1/file.php should become www.example.com/file
Restrict Folder browsing - I want to restrict folder browsing capability when somebody fires an URL eg. www.example.com/subfolder Conventionally by firing this URL user will be able to browse through the contents of subfolder. By firing such URL or any URL containing domain example.com eg. www.example.com/folderNotExist then server should redirect to index page.
I am able to restrict folder browsing but redirection to index page and hiding of folder and file extension is not working.
You can have these rules in your root .htaccess:
ErrorDocument 404 /
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{THE_REQUEST} /subfolder/subfolder1/ [NC]
RewriteRule ^ / [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/subfolder/subfolder1/$1.php -f
RewriteRule ^(.+?)/?$ subfolder/subfolder1/$1.php [L]
You should try something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /subfolder/subfolder1/$1.php
ErrorDocument 404 /index.php
you can try this
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.mysample.com
RewriteRule ^subdir/(.*)$ http://www.mysample.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f

.htaccess mod_rewrite in main directory and subdirectory

I've a main directory with a .htaccess file that looks like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
The one in the subdirectory looks identical to this one. What I want the .htaccess files to do is redirect to the index file in each directory.
So if you visit main/example you get redirected to the index.php file in the main directory but if you visit main/subdirectory you get redirected to the index.php file in the subdirectory.
I've searched for an answer but haven't found anything that works.
Thankful for any help!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/ /$1/index.php [L]
This configuration do the following:
if we have this files
/index.php
/foo/index.php
/var/index.php
/var/boo/index.php
and you request the follow file
/foo/hello
you will go to:
/foo/index.php
and, if you request
/var/boo/world
you will go to:
/var/boo/index.php
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 -d
RewriteRule ^(.+)/ $1/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
You can use a simple solution like this one, no need to put .htaccess files everywhere
on your main directory you put one .htaccess file, you keep your code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
and you add those two lines
Options All -Indexes
ErrorDocument 403 http://domain.com/index.php
these two lines prevent the user to access the folder, and returns the statut 403 and then you redirect your 403 statut to the adress of your index.php of your main directory

.htaccess that redirects to index.html, or to index.php if there are parameters

I am looking for a .htaccess that does the following:
For users that visit mydomain.com, the server should return the index.html file from the root.
For users that visit mydomain.com/something/, mydomain.com/anything/123/, mydomain.com/some%20encoded%20text, etc., the server should return the index.php file from the root and pass the text after www.mydomain.com/ as a PHP $_GET variable.
I tried the WordPress .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
But this redirects everything to the index.php file.
Enable mod_rewrite and .htaccess through httpd.conf (if not already enabled) and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^$ /index.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?f=$1 [L,QSA]
Inside index.php you can access $_GET['f'] variable that will give you requested URI.

.htaccess - works only with index.php/ not with /

I have an test project in sub directory (G:/wamp/www/test) which has only index.php and .htaccess
I tried to make friendly urls by adding RewriteRule to .htaccess like this:
RewriteEngine on
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteRule ^/*$ index.php
This worked on all other projects but it is not working on this one. When I want to access an URL like this:
http://localhost/test/TESTING_URL I am getting default 404 Not Found page. However, if I go like this: http://localhost/test/index.php/TESTING_URL - it works.
Apache error log is giving me the following error:
File does not exist: G:/wamp/www/test/TESTING_URL
The following worked for me:
RewriteEngine on
RewriteBase /test/
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteRule ^/*$ index.php
Make sure that this .htaccess file is in the directory test and that there aren't any conflicting rewrite rules in any .htaccess file at a higher level.
Otherwise the only difference is the addition of the RewriteBase line.

Categories