Rewrite url rules in .htaccess inside a directory - apache

I'm trying to use rewrite url rules using a .htaccess file. This seems to work fine in my main .htaccess file (only works for php pages which exist in same directory), but when I want to use another .htaccess for php file inside a subdirectory the rules don't apply at all and I get a 404 error page.
The file I want to point to is /news/story.php. story.php requests an integer variable called 'article' to fetch the article from database and display it.
So basically what I want to do is replace http://www.mydomain.com/news/story.php?article=1
with http://www.mydomain.com/news/story/article/1/
I'm using the following rules in the .htaccess file which is inside the 'news' directory.
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule story/article/(.*)/ story.php?article=$1
RewriteRule story/article/(.*) story.php?article=$1
Thanks for your help

RewriteRule story/article/(.*)/ news/story.php?article=$1
RewriteRule story/article/(.*) news/story.php?article=$1

Related

Allow subfolders along with website with .htaccess of Ghost installation

In my Ghost installation on server I want to access a few folders and subfolders, but the main ghost setup's htaccess prevents that. I don't understand htaccess at all. What rewrite rules should be used to allow a few subfolders.
example.nl should be accessible along with all permalinks. Then a particular folder with a few subfolders need to open when path is requested from browser. That's not happening right now. I will need to load index.html files with all css and js etc.
Example: example.nl/subfolder/sub1/index.html
Options +FollowSymLinks -Indexes
IndexIgnore *
DirectoryIndex
<IfModule mod_rewrite.c>
RewriteEngine on
# Simple URL redirect:
RewriteRule ^(.*)$ http://example.nl:62254/$1 [P]
</IfModule>

url rewriting "double path for same file"

I have an issue with my htaccess file but I dont understand where the mistake comes from:
Ex:
I have the following file in my folder
contact.php
I did a basic rewrite like that
RewriteRule ^about/$ contact.php [L]
so now my contact page is accessible from http://localhost/project/about/
the problem is that page is also accessible from
http://localhost/project/contact/
Why is this happening?
How can I disallow that?
Thanks for all your answers!
That is due to enabling of option MultiViews. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
To disallow this you can add this line on top of your .htaccess:
Options -MultiViews

How To Use Htacess for www.example.com/type.php?user=name

I want to redirect www.example.com/type/name to www.example.com/type.php?user=name
{
RewriteRule ^([a-zA-Z0-9_-]+)/type$ type.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/type/$ type.php?key=$1
}
Please help me about this.
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^type/([^/]+)/? type.php?key=$1 [L]
This assumes that mod_rewrite is both installed and activated for .htaccess files. If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo(); By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All
try this , hope it helps :
RewriteBase /www.example.com
RewriteEngine On
RewriteRule ^type/(.*)/$ /type.php?user=$1
#OR
RewriteRule ^(.*)/(.*)/$ /$1.php?user=$2
PS : The first one works only for the url : www.example.com/type/value ,the second one works for any page in your site ex: www.example.com/anypage/value

mod_rewrite URI with same name as folder or files

A simple .htaccess file with this:
RewriteEngine On
RewriteBase /
RewriteRule ^webshop$ /index.php?page=webshop [L]
... does not work for me because a have a file called webshop.php in the web root. Renaming the file solves the poblem, and changing the regex in the .htaccess file solves the problem, but still - it's only a partial match of the file name...? The only thing I can find on this is to use
DirectorySlash off
I've tried that and it made no difference.
Need some help here, there must be a pretty simple solution to this.
Most likely you have MultiViews options enabled. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
Disable it by placing this line on top of your .htaccess:
Options -MultiViews

Hide *.inc.php from website visitors

I have a script myscript.inc.php which handles all urls that look like /script-blah
I accomplish this by using following .htaccess
RewriteEngine On
RewriteRule ^script-(.*)$ myscript.inc.php?s=$1 [QSA,L]
However users could also access it this way by typing /myscript.inc.php?s=blah
I would like to prevent that. I tried
<Files ~ "\.inc\.php$">
Order deny,allow
Deny from all
</Files>
and
RewriteCond %{REQUEST_URI} \.inc\.php
RewriteRule .* - [F,L,NS]
They both prevent users from viewing /myscript.inc.php?s=blah but they also cause /script-blah to return 403...
Is there a way to do this correctly?
I use the following method to protect my .inc.php files. Add the following to your .htaccess:
#Prevent Users From Accessing .inc.php files in .htaccess
<Files ~ ".inc.php$">
Order allow,deny
Deny from all
</Files>
You could also try the following (a number of open source packages do this)
place a blank index.html in every folder
use this rule in .htaccess to block folder reading Options -Indexes
place a line that dies scripts where a global constant isn't found
For example, here is Kohana's "toss out invalid accesses". It is the first line in all PHP files.
<?php defined('SYSPATH') or die('No direct script access.'); ?>
This line basically says "if not included via index.php where SYSPATH is defined, we will abort script and show a friendly message"
You could redirect if it is a filename
RewriteCond %{REQUEST_FILENAME} =-f