Mod-Rewrite and .htaccess Problems - php

Having some issues with Mod_Rewrite. I must admit this is my first venture into the module so I could be going through something silly. I have been trying to get this to work on my own for a day or so off and on and I can not figure out why this is not working.
I am getting the following errors:
Notice: Undefined index: Dir in D:\wamp\www\WildForFashion\Portal\Index.php on line 31
What I am trying to accomplish is to be able to have one template page (index.php) and have Mod_Rewrite change the url for me so it can be Search Friendly and to also ease coding and design.
I was also having issues when it came to no Images / CSS files loading at all, I did read that by using an absolute url would work.
As of right now I am working in the PORTAL directory first (which is an admin page for website maintenance) and then will be moving onto the root directory. I am not sure if I need to have one .htaccess file or two (one being in root dir, second being in portal dir).
The DIR folder is to hold each category and page for the website like the following:
Domain.com/{CATAGORY}/{PAGE}/
ROOT / DIR / {CATAGORY} / {Page}.php
Domain.com/Portal/{CATAGORY}/{PAGE}/
ROOT / Portal / DIR / {CATAGORY} / {Page}.php
.htaccess Before
RewriteEngine on
RewriteRule ^/Portal/$ Portal/Index.php?Dir=Portal&Page=Home [L,QSA]
RewriteRule ^Portal/(.*)/(.*)$ Portal/Index.php?Dir=$1&Page=$2 [L,QSA]
RewriteRule ^Portal/(.*)/$ Portal/Index.php?Dir=$1&Page=Home [L,QSA]
.htaccess After (Working w/ Exceptions)
RewriteEngine on
RewriteRule Portal/$ Portal/Index.php?Dir=Portal&Page=Home [L,QSA]
RewriteRule Portal/([^/]+)/$ Portal/Index.php?Dir=$1&Page=Home [L,QSA]
RewriteRule Portal/([^/]+)/([^/]+)/$ Portal/Index.php?Dir=$1&Page=$2 [L,QSA]
Index.php
<?php
include_once("DIR/" . $_GET['Dir'] . "/" . $_GET['Page'] . ".php");
?>
Directory Tree
CSS
DIR
IMG
JS
SRC
Portal
CSS
DIR
Portal
Inventory
Stats
Orders
IMG
JS
SRC
Index.php
.htaccess
Index.php

You are including forward slashes in the matching rule that don't need to be there and excluding them from the redirect path where they do need to be. Try this:
RewriteRule Portal/$ Portal/Index.php?Dir=Portal&Page=Home [L,QSA]
RewriteRule Portal/(.*)/(.*)$ Portal/Index.php?Dir=$1&Page=$2 [L,QSA]
RewriteRule Portal/(.*)/$ Portal/Index.php?Dir=$1&Page=Home [L,QSA]
Note I removed the ^ from the matching portions of the rewrite rules as this indicated taht should be the beginning of the URI. I also removed the "/" in front of the substritution pattern.

Related

RewriteRule for friendlier URLs

I have a site that has a news section in a subfolder which I have been adding to manually.
www.example.com/subfolder/news-and-views/name-of-article.php
I recently made a rather simplistic CRUD feature to make adding these articles easier.
I have:
www.example.com/subfolder/news-and-views/index.php - This displays all articles
www.example.com/subfolder/news-and-views/article.php - This takes an Id parameter from a $_GET request
The url I end up with when I view an article is:
www.example.com/subfolder/news-and-views/article.php?Id=name-of-article
I did some reading and used http://htaccess.mwl.be/ to test a RewriteRule
The rule is: RewriteRule /news-and-views/(.+) /news-and-views/article.php?Id=$1.php [L]
However when I go to news-and-vies/ (the index page) my CSS is stripped out and nothing is displayed as my paths are changed.
Also it seems to go to article.phpid?= without passing anything, rather than just using the actual index page.
To set paths I use a variable called $path2root which is the number of directories to the root.
E.g $path2root = "../../";
So: I'd have include $path2root . "includes/head.php"
Is my RewriteRule interferring with this variable?
My .htaccess file
RewriteEngine On
RewriteRule /news-and-views/(.+) /news-and-views/article.php?Id=$1.php [L]
My file structure
/ .htaccess at this level (root)
--/ subfolder
----/ news-and-views
------/ index of news and views
RewriteRule /news-and-views/(.+) /news-and-views/article.php?Id=$1.php [L]
As mentioned in my comment, you appear to have omitted the /subfolder, so I'm not sure how this rewrites successfully at all? You also need to be careful of rewriting existing files (eg. index.php and article.php and CSS?) and creating a rewrite loop.
Try something like the following instead:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(subfolder/news-and-views)/(.+) /$1/article.php?Id=$2.php [L]

RewriteRule for a single .php page in a sub directory

I've a joomla website. Recentley I added a subdirectory to the root of the website which contains some pages which are not linked to the CMS in any way.
I wanted to remove the .php from the end of these pages as I was going to promote them over social media and wanted the URL's to be easier for users to remember.
The page in questions is:
http://www.mytestwebsite.com/share/thepage.php
So I added the following rule to my .htaccess file:
RewriteRule ^/share/thepage?$ /share/thepage.php [NC]
With the hopes that the URL would be http://www.mytestwebsite.com/share/thepage
and still load thepage.php but it's not working.
Use:
RewriteEngine on
RewriteRule ^share/thepage/?$ /share/thepage.php [NC,L]
No leading / in the first RewriteRule argument in htaccess.
And /? is for the optional trailing slash (not optional e with e?)

How do I get mod-rewrite to access the proper directory?

I'm trying to use mod-rewrite to eliminate urls like {root}/index.php?action=about&page=faq. I want {root}/about/faq to redirect to the php url. My .htaccess file looks like
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(index|about|calendar|news|contact)$ index.php?action=$1 [L]
RewriteRule ^(index|about)/(.+)$ index.php?action=$1&page=$2 [L]
When redirecting to index.php?action=$1, this works fine. However, it doesn't work completely when the second rule is used. The html content appears fine, but the stylesheet and images are missing. The page seems to think that it's in a subdirectory. So {root}/about/faq looks for images in the folder about/images, instead of in the images folder found at the root. The PHP script says that the working directory is still the root directory, however. How do I make the pages think that they are in the root directory? Thanks!
RewriteRule ^(index|about|calendar|news|contact)/(.*)/$ /index.php?action=$1&page=$2
Would result in /about/faq/ rewriting to index.php?action=about&page=faq
/calendar/month/ would result in index.php?action=calendar&page=month
Also unless you want to rewrite imagesfiles I would add:
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
This will stop image requests being rewritten.

Use .htaccess to prevent root directory from public access

I don't know such thing is possible or not practically, but let's give it a try. In past (more than three years ago), I came to know about solution like this (don't know exactly how it is), in one MVC tutorial.
Directory structure
WebRoot/
includes/
config/
public/
images/
js/
css/
index.php
init.php
What I Want To Do Is
When user access http://www.mysite.com/a/b/c/, redirect all requests to /public/index.php page. i.e., all requests to website will go through /public/index.php page, except images/, js/ and css/ directories, which are in public/.
Also, http://www.mysite.com/includes/ will redirect to index.php page, and get includes/ as $query.
Index.php page
<?php
// get init file from webroot directory, which will load all required php files.
define ('ROOT', dirname(dirname(__FILE__)));
require_once (ROOT . 'init.php');
// get query from url (here, $query = "a/b/c/")
if(isset($_GET['q'])) {
$query = $_GET['q'];
}
// insert images, css etc. resource
echo "<img src='http://www.mysite.com/images/foo.jpg' />";
echo "<script src='http://www.mysite.com/js/bar.js'></script>";
?>
This will be useful for them who are using web-hosting, which doesn't provide one step up directory access to webroot.
EDIT
OK, I found the site from where I got this concept. See two .htaccess files used in root directory and public directory. Link to MVC tutorial
I assume the .htaccess is in WebRoot.
To rewrite all requests, you just use RewriteRule. When you want to exclude some paths, you use one or more RewriteCond
RewriteEngine on
RewriteCond %{REQUEST_URI} !/images/
RewriteCond %{REQUEST_URI} !/css/
RewriteCond %{REQUEST_URI} !/js/
RewriteCond %{REQUEST_URI} !/public/index\.php
RewriteRule .* public/index.php?q=$0 [L,QSA]
RewriteRule ^images/.+ public/$0 [L]
RewriteRule ^js/.+ public/$0 [L]
RewriteRule ^css/.+ public/$0 [L]
The flag QSA appends any other existing query string as arguments to index.php. If you don't want to append any existing query string, just leave the QSA flag out.
If you want the rewrite to be visible to the client, you must add the R flag to the RewriteRule, i.e. [R,L,QSA] or [R,L].
You can use another tricks, where your page will not redirected to another page but you will restrict other user to see the directory index
Options -Indexes
Add this text in your .htaccess file, it will generate a 403 errror (forbidden)

.htaccess in subdirectory

I've been searching the whole evening for a solution/approach for my problem with my .htaccess file.
Basically I have www.site.com with a .htaccess in the www directory with the following content:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(/admin)
RewriteRule ^[^_+/]. index.php
It works just fine and allows www.site.com/en/articles/Interesting_title/3 to be parsed by index.php which reads which controller, language and what article to display.
However I'd like for the admin system to work the same way. www.site.com/admin should have an .htaccess files that allow me to write URL's this way.
www.site.com/admin/en/articles/article_title/3 should allow me to edit article number 3 using en english UI.
Dumping $_SERVER['REQUEST_URI'] in the first case gives "en/articles/Interesting_title/3"
Dumping $_SERVER['REQUEST_URI'] in the second case gives "admin/en/articles/article_title/3"
If I at a later point choose to move administration to www.site.com/shassooo I would like to avoid changing any code other then the .htaccess files.
Cheers
Append this line in the same .htaccess file you have, not under admin sub directory:
RewriteCond %{REQUEST_URI} !^/admin/index.php$ [NC]
RewriteRule ^admin/[^_+/]. /admin/index.php [L,NC]

Categories