URL forwarding to index page - php

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.

Related

change permalink through .htaccess

i want to change my permalink example www.testing.com/1234/"titlename" instead of www.testing.com/1234/, how to do i change it through .htaccess? Below are my .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /clients/ohmynews/web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?article/([a-zA-Z0-9_-]+)$ detail.php?did=$1
RewriteRule ^/?([a-zA-Z0-9_-]+)$ listing.php?lid=$1
RewriteRule ^/?search/([a-zA-Z0-9_-]+)$ search.php?keys=$1
DirectoryIndex index.php
ErrorDocument 404 http://wipstage.com/clients/ohmynews/web/
</IfModule>
Include this line into your htaccess file:
RewriteRule ^([0-9]+)/([A-Za-z0-9._-]+)/?$ detail.php?id=$1&title=$2
As i do not know your actual running file path, so make sure you put your original running file path in my code.
Or let me know your file name with location, i will edit my answer.

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

Apache redirect all to index.php except for existing files and folders

I have index.php that reads full path by $_SERVER[‘REQUEST_URI’] variable. My task is when user enter: www.domain/resource/777 redirect to index.php with path /resource/777 and parse $_SERVER[‘REQUEST_URI’] to do some logic. But I have also real files and folders like:
css/theme.css
assets/assets.js
resource/locale.js
When I try this config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L]
Client could not get all css and etc files. How to write correct rule in Apache to achive this?
If you are using Apache 2.2.16 or later, you can replace rewrite rules entirely with one single directive:
FallbackResource /index.php
See https://httpd.apache.org/docs/2.2/mod/mod_dir.html#fallbackresource
Basically, if a request were to cause an error 404, the fallback resource uri will be used to handle the request instead, correctly populating the $_SERVER[‘REQUEST_URI’] variable.
Your rule is fine. Issue with css/js/image is that you're using relative path to include them.
You can add this just below <head> section of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
Also keep just this rule in your .htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]+$ index.php [L]
According to the documentation REQUEST_FILENAME only evaluates to the full local filesystem path to the file if the path has already been determined by the server.
In a nutshell this means the condition will work if it is inside an .htaccess file or within a <Directory> section in your httpd.conf but not if it's just inside a <VirtualHost>. In this latter case, you can simply wrap it in a <Directory> tag to get it working.
<VirtualHost *:80>
# ... some other config ...
<Directory /path/to/your/site>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L]
</Directory>
</VirtualHost>
You can set the error document in your htaccess file so that index.php will be used then a non existing file is requested:
ErrorDocument 404 /index.php
For now only this helped to me:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/src/
RewriteCond %{REQUEST_URI} !^/assets/
RewriteCond %{REQUEST_URI} !^/resource/
RewriteCond %{REQUEST_URI} !^/data.json
RewriteRule ^(.*)$ /index.php [L]

htaccess not get index file after rewrite rule

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]

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