i am using htaccess for dynamic url rewriting. i want to show the test.php as test.html
the htaccess code is
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php [nc]
after running test.php, the browser show test.php on address bar. but if i change test.php as test.html the page give the same content. my apache config file are fine. how can i solve this?
thanks to help me..
You cant deny access to your .php
Yon need to put in your a tag the html link and it will go to php in the background.
If you still want to sow only the html address you can put regex in your php file and check if the extension is php. if so redirect the page to html.
example:
<?php
$temp = $_SERVER['PHP_SELF'];
$temp = explode('.', $temp);
if($temp[count($temp)-1] == "php"){
header("Location: test.html", true, 301);
}
?>
* not tasted !!! *
Related
I have a website and I want the redirect all files to index.php file. But I have problem with folders. domain.com/panel/ doesnt work. but domain.com/panel/index.php works.
in localhost with wampserver, works good. But in hosting doesnt work.
Here is my htaccess file:
RewriteEngine On
RewriteBase /
# Site yönlendirme motoru
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-\s]+)/?$ index.php?url=$1 [NC,L,QSA]
RewriteRule ^(.*?\.(?:html?|php))$ index.php?url=$1 [QSA,L,NC]
Not: I want only html and php files because, if i dont do this, images, css, js files doesnt work. Is there any another way? How can i redirect all files to index.php while images, css, js files still works?
Not 2: I want panel folder to redirect because of security. like this:
$url = $_GET['url'];
$url_array = explode('/', $url);
if($url_array[0] == 'panel') {
if($url_array[1] != 'login.php' and (!isset($_SESSION['user']))){
header('Location: login.php');
die();
}
include($url);
}
edit:
What i have to make for except to images, css, js files on htaccess?
my current url : http://example.com/blog-single.php?id=15
this is a dynamic blog page the "id" has to be change accordingly to blog adding
I need to change this url to http://example.com/blog-single/<coresponding-blog-name>
eg: http://example.com/blog-single/my-new-blog
how to Remove .php and id from the current URL?
First of you need to create a .htaccess file and add this code in it
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ blog-single.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ blog-single.php?id=$1
After that in your main file for example in index.php file you need to add the code
<?php
$key=$_GET['key'];
if($key=='my-new-blog')
{
include('my_new_blog.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else
{
include('users.php'); // Users Gateway
}
?>
try this according to your need.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)$ $1.php?id=$2 [NC,L]
here you can use your filename in manner http://example.com/blog-single/my-new-blog this will work for every page as per your requirement.
For example, if you access to this: http://example.com/blog/1234.html and real path is http://example.com/blog.php?id=1234 you can put this on your htaccess file:
RewriteEngine On
RewriteRule ^blog/([^/]*)\.html$ /blog.php?id=$1 [L]
This tool will help you: http://www.generateit.net/mod-rewrite/index.php
I have my query string to include my pages, in my homepage like you see below and it is working fine, Im including my pages fine.
But something wrong is happening and Im not finding how I can solve this.
I will try to expain my isse with an example: I have a folder "teachers" inside I have two pdf documents and a page "documents.php".
To acess this documents page, Im acessing: "htp://localhost/website/teachers/documents", and it is working fine.
But If I acess "htp://localhost/website/teachers/", Im able to acess my pdf documents and my page as you see in my image below.
But I dont want this, I want that If some user tries to acess "htp://localhost/website/teachers/", I want to include my 404 file (require_once('inc/404.php');)
My query string:
#$url = $_GET['url'];
$url = explode('/', $url);
$url[0] = ($url[0] == NULL ? 'index' : $url[0]);
if(file_exists('inc/'.$url[0].'.php')){
require_once('inc/'.$url[0].'.php');
}
elseif(file_exists($url[0].'/'.$url[1].'/'.$url[2].'.php')){
require_once($url[0].'/'.$url[1].'/'.$url[2].'.php');
}
elseif(#file_exists($url[0].'/'.$url[1].'.php')){
require_once($url[0].'/'.$url[1].'.php');
}
else{
require_once('inc/404.php');
}
Do you see what Im doing wrong to not be having the result I want?
My htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
There is an easier solution.
Add this line to the beginning of your .htaccess file:
Options -Indexes
This way, you won't be able to see the folder contents.
EDIT: htaccess rule solution (which is in website folder)
ErrorDocument 404 /website/inc/404.php
RewriteEngine On
RewriteBase /website/
RewriteRule ^teachers/$ - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
I would like to rewrite all my URL's like so:
http://example.com/folder/40/name/test.php
and
http://example.com/folder/40/test.php
to
http://example.com/test.php
Does anyone have a solution?
thanks
You can do that by using htaccess. It will redirect all urls like
http://example.com/folder/40/name/test.php
http://example.com/folder/40/test.php
to
http://example.com/test.php
In the htaccess file add below lines
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteRule ^(.*)/(.*)/(.*)/test.php$ test.php [L]
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteRule ^(.*)/(.*)/test.php$ test.php [L]
more information Htaccess Rewrites
You can easily do it by PHP but you have to include this code in both pages before opening HTML tag.
<?php
$useragent=$_SERVER['HTTP_USER_AGENT'];
header('Location: http://example.com/test.php'); ?>
Hope this will help you!
and thanks in advance for any help!
What I'm trying to achieve is this: I would like to remove the file extension from file names using htaccess - which is what I've done. However, when a user points their browser to a file without an extension and adds a trailing slash I get a page not found error. I found some code which corrected this, however, when I implemented the code, it broke my relative links to CSS and jQuery located in the head.
I really would rather like to keep relative links to all my files, but would also like to allow trailing slashes for individual files.
Is this possible?
My site is here: http://www.getagig.info
And my current htaccess code (which only removes extensions is below).
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Perhaps try:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ $1.php
Edit: I understand from your comment below what your issue is, but I've taken a look at your site, and I'm a little confused why you're trying to do it this way.
From what I understand, all you really want is for yourwebsite.com/filename or yourwebsite.com/filename/ to use filename.php, in which case you're better off using an .htaccess rule like the following:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ $1.php
This should still allow your CSS and JS files to load, as well as any file or directory that physically exists on your server. If you do not want people to physically call "filename.php", then you can set-up a separate rule for that.
I don't think it's possible to test if the file exists in RewriteCond if you have / appended to your url.
Only solution I can think of here would be using htaccess to redirect to some php file which will sort things out:
.htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*?)/?$ route.php?url=$1
.php file
<?php
$url = $_GET['url'];
if (strpos($url, '..') !== false || strpos($url, '/') !== false) {
echo "No relative urls!";
} else if (file_exists($url)) {
// do what you want - require or redirect to the requested file
} else {
// requested file does not exist
}
Adding the line:
<BASE href="http://www.sitename.com/">
to my page inside the <head> tag works great for me. It can be coded even a PHP function to automatize this so you don't have to bother with copy/paste on other occasions.
Update:
<?php
$BaseDir = 'http://'.$_SERVER['SERVER_NAME'].substr($_SERVER["SCRIPT_NAME"], 0, strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
?>
<head>
...
<BASE href="<?php echo $BaseDir; ?>">
...
</head>