Htaccess doesn't redirect folders - php

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?

Related

When I host my website I can acess homepage but I always get 404 error when I try to acess my secondary pages [duplicate]

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]

Url rewriting for all identical filenames

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!

Url rewriting using .htaccess file on php

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 !!! *

custom .htaccess rewrite rules

I want to redirect all to one script e.g. index.php?url=inputurl
With if/else I want to parse url
in index.php run query for url in my custom table
if url is mach: echo "ok"
else do nothing
How should I set .htaccess in root folder of Wordpress?
Example:
URLs in custom_table:
asd
dfg
ghj
If user puts:
www.mysite.com/asd
-> mod_rewrite should output this: www.mysite.com/index.php?url=asd
Else if user puts:
www.mysite.com/zzz
-> do nothing
I think the following .htaccess should solve the problem:
RewriteEngine On
# Redirects everything that is not index.php to index.php
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php?url=$1 [L,R]
Edit: to not include your folders and files (like /js, /css, etc.) in rewrite, add the following lines before the RewriteRule line (see comments):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
And in the PHP script:
$url = $_GET['url'];
// the method is_valid should check if the page exists in DB
if (is_valid($url)) {
// do something here
// maybe redirect with header('Location: path')
} else {
// show a not found page (error 404)
}
You want to both be able to read from a database and do nothing if there is not match.
This would require you run code to access db then return back to apache to process and is not possible from .htacccess (though it is from httpd.conf).
The .htaccess solution would be to specify all the "table" entries inline as below.
RewriteEngine on
RewriteBase /
#if asd or dfg or ghj
RewriteCond %{REQUEST_URI} ^/(asd|dfg|ghj) [NC]
RewriteRule . index.php?url=%{REQUEST_URI} [L]

htaccess - removal of extension - allow or force trailing slashes without breaking relative links

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>

Categories