htaccess not get index file after rewrite rule - php

My htaccess is this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([0-9a-zA-Z-]+)?$ show.php?id=$1 [L]
</IfModule>
So whenever browser get 'any thing' after root folder then it will go show.php and parameter will be 'any thing' .
Note: it will not effect if it have extension or a folder.
But there is index file in my root folder. So browser should take index file first. So that, i added this in .htaccess file :
DirectoryIndex index.php
But not working. So is there anything to do, to show at least index file whenever root folder visited ?

You can have your .htaccess like this:
DirectoryIndex index.php
RewriteEngine On
# request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
# request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/?$ /show.php?id=$1 [L,QSA]

Related

Redirect requests with .htaccess to api.php

I've created a folder api/ in my www/html folder.. I've put a .htaccess file in there that looks like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule api/(.*)$ api/api.php?request=$1 [QSA,NC,L]
</IfModule>
In the api/ folder I have the api.php file with this code for testing;
<?php
echo $_REQUEST['request'];
?>
If I go to myserver/api/test I get a 404 not found.. So in this case I would thing it should echo test?
I've also tried putting the file in the www/html file instead of inside the api folder since I specify the api folder in the .htaccess file..
I had to set AllowOverride All in my apache2.conf file
Reference this answer:
https://stackoverflow.com/a/15662459/7417114

Get the latest part of the URL as an input to a PHP file

I only have two files at my directory of http://www.example.com/members/: an .htaccess file and an index.php file:
The contents of the .htaccess file:
RewriteEngine on
RewriteRule (.*) index.php?code=$1
The contents of the index.php file:
<?php var_dump($_GET);
I also have a bunch of URLs in the format of "http://www.example.com/members/nnnn". I just want to get the nnnn part in my PHP file while having the URL intact.
But now, if I request the URL of http://www.example.com/members/13, I would like to get the 13 in my index.php file; while, what I get instead, is:
array(1) { ["code"]=> string(9) "index.php" }
The 13 is not there!! So, how do I change the .htaccess code to get the desired behavior?
Thanks in advance :)
To have more space than using the comments I did above:
I would write the .htaccess file as:
RewriteEngine On
# if given url does point to exsiting Dir or File:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
# Then do nothing and stop
RewriteRule ^ - [L]
# still here: so go to index.php to execute
RewriteRule ^.* index.php [L,QSA]
In index.php, I would then check $_SERVER['REQUEST_URI'] to find the information. I leave you with strrpos(), substr() or even explode() to do your thing.
Try this in the .htaccess file:
Make sure your .htaccess and index files are in a same folder.
# set directory index
DirectoryIndex index.php
# No directory listings
IndexIgnore *
<IfModule mod_rewrite.c>
# Uncomment the following line if your webserver's URL is not directly related to physical file paths.
# Also uncomment it if you are on 1&1 hosting
#RewriteBase /
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !
RewriteRule ^([a-zA-Z0-9]+)/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?$ index.php?code=$1&code2=$2&code3=$3 [NC,L]
RewriteRule ^$ index.php [L]
</IfModule>
And if you want more levels of paths; then, just add the following before the $ sign in the last RewriteRule:
/?([a-zA-Z0-9]*)?
Using that, the URL of:
www.example.com/members/13
will become:
www.example.com/members/index.php?code=13

hiding directory name in the URL

my site's directory is:
localhost/
includes/
includes/initialize.php
includes/user.php
includes/session.php
includes/database.php
public/
public/bootstrap/
public/css/
public/js/
public/dashboard/index.php
public/index.php
.htaccess(first htaccess file)
{
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1 [QSA,L]
</IfModule>
}
I'd like to have my url more neat, for example
when I type this: http://localhost I know it goes to http://localhost/public/
but the URL still is http://localhost
but the problem is when I type http://localhost/dashboard
unfortunately the URL will be http://localhost/public/dashboard/ on my browser.
I do not want to see the word public.
I think I should have another htaccess file under the public directory, but I do not know the suitable code for it.
Try this code in your DOCUMENT_ROOT/.htaccess file:
DirectoryIndex index.html index.php
RewriteEngine On
RewriteRule !^public/ /public%{REQUEST_URI} [NC,L]

URL forwarding to index page

Okay so when users type in www.shareit.me/matt i want it to take them to www.shareit.me/index.html How can I do this?
Here is a .htaccess file I commonly use. I have it forward to index.php, but changed it to .html and it should work the same.
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.html
RewriteRule . index.html
This rule in your .htaccess file should be enough:
RewriteEngine on
RewriteRule ^matt/?$ index.html [NC,QSA]
Place the file in the document root.

how can I set .htaccess for existing files and folders

Can any one help me in .htaccess.
My use case is, if there is index.php in the directory http://domain_name/user/ then load index.php else set rewriteRule to call forbidden.php.
I did a bit but not succeed yet. .htaccess code on my server is-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond ^(.*)$/index.php !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.*)$ forbidden.php?handle=$1
</IfModule>
I think there is an easier way to do what you want. There is no need for rewriting, simply define which page acts as the entry page. In the .htaccess file you can define:
DirectoryIndex index.php
ErrorDocument 404 /forbidden.html
Now, if anybody calls your directory http://www.example.com/user/, the page http://www.example.com/user/index.php will be shown.
If the file index.html does not exist, the server will return an error, so you can define an appropriate error page. With a leading /, you can define a single error page in the root directory, without the / it will look in the relative directory http://www.example.com/user/forbidden.php.

Categories