Help with mod_rewrite - mod-rewrite

I have my site under the structure: www.somesite.com/index.php?page=p_section_page.php
I want my site to display something like: www.somesite.com/section/page OR just www.somesite.com/page/
Tried to write rules in the .htaccess file like so:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule PAGE index.php?page=p_section_page.php
It works fine if i try www.somesite.com/page/ but the images, css files and js, try to access the www.somesite.com/page/css/ or www.somesite.com/images/ directories which clearly do not exist.
Any help here? Thanks!

My educated guess is that you are using relative paths. If you change the URL of the HTML document you change the effective path of pictures, scripts and other resources that are loaded via relative paths. It's the same as when you move the file to another directory.
I find it easier to call resources with absolute paths. You are using PHP so you have two options: hard-code the leading slash or use PHP to set it:
<img src="/images/picture.jpg" ... >
<img src="<?=$_SERVER['DOCUMENT_ROOT']?>images/picture.jpg" ... >

Related

mod_rewrite rule for file to be downloaded from another directory

Note: I couldn't find any similar question. Please let me know if duplicate question exists. I don't want to put duplicate question intentionally.
I have directory structure longer which I don't want users to find out as well as I want download file URL to be fancy.
My directory structure is
Actual File Path = ./my_dir/files/upload/user_images/user_file.pdf
My Domain is having ./my_die/ as Document Root so example.com will serve contents of ./my_dir/
To download file I have http://example.com/files/upload/user_files/user_file.pdf
Which I want to convert to
http://example.com/user/uploads/user_file.pdf
So I need MOD Rewrite Rule to convert as following:
http://example.com/files/upload/user_files/user_file.pdf to http://example.com/user/uploads/user_file.pdf
You may use this rewrite rule in your site root .htaccess:
RewriteEngine On
RewriteRule ^user/uploads/(.+)$ files/upload/user_files/$1 [L,NC]

mod_rewrite from a subfolder with a querystring to a folder or file

I am trying to redirect from:
http://www.example.com/folder/product.aspx?prodid=146
to
http://www.example.com/folder2/folder3/
The folders referred to here don't really exist. There are other rewrite rules in place which redirect transparently to the actual content.
If I create a directory called 'folder', and put an .htaccess file in it, I can get the redirect working, BUT, other URLs which refer to that folder no longer work. So I have to try and do the redirect from the .htaccess file in the ROOT folder.
I tried this:
RewriteCond %{QUERY_STRING} prodid=146
RewriteRule ^/folder/product.aspx$ /folder2/folder3/? [R]
...but it doesn't work (I get a 404 error). Using identical syntax but omitting the /folder/ from the 2nd line works if the .htaccess is in the folder directory (so I know the above can't be too far off) - but as I said, I cannot do that. I have tried lots of variations but nothing seems to work. Any assistance appreciated.
You need to remove the slash from the start your URL regexp. Like this:
RewriteRule ^folder/product.aspx$ /folder2/folder3/? [R]

mod_rewrite help

I want to hide my directory structure and have all requests served out of a single directory where my files are located. The actual path where my files are stored is: http://mydomain.com/dir1/dir2 but I'd like to be able to just point my links to just http://mydomain.com/myscript.php.
I have multiple script in this directory so I'm not sure how to go about this. Would I need a rule for each and every file I need access to or is there a wildcard that I can use for this?
You can do this:
RewriteCond %{DOCUMENT_ROOT}/dir1/dir2%{REQUEST_URI} -f
RewriteRule !^/dir1/dir2/ /dir1/dir2%{REQUEST_URI} [L]
This will rewrite any request, that’s path does not begin with /dir1/dir2/, to the corresponding location with that prefix /dir1/dir2/ but only if there is a file at that destination (see RewriteCond directive).

CodeIgniter CSS File Not Included

I have a bit of an odd problem with my CodeIgniter installation, I am using modrewrite in order to shorten my URLs, for example I have an api page:
http://www.mydomain.com/api
And it works really well, my .htaccess file looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
But whats really strange is if I type a trailing slash:
http://www.mydomain.com/api/
The API page loads ok but it doesn't include the Base CSS file which is included on all the pages, it would be fine if it didn't load anything at all, but I need to stop it from loading the actual api page. Any advice would be helpful,
Thanks!
UPDATE: I have found that when I view source my CSS file comes from the following when the CSS loads correctly:
http://www.mydomain.com/css/all.css
But when it fails to load it comes from
http://www.mydomain.com/api/css/all.css
Make sure that you have a trailing slash in your base_url in the config.php file:
$config['base_url'] = "http://www.mydomain.com/";
And always call the base_url() when dealing with links...etc
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/all.css">
EDIT:
Please note that I'm using the mod_rewrite from this wiki page BUT since my CI installation in not on the root (inside /ci173/) the RewriteBase is not / but /ci173/.
When using relative URL path like css/all.css or ./css/all.css (both are equivalent), you need to be aware that relative URL paths (like any relative URLs) are resolved using a base URL that is the URL of the current document if not specified otherwise.
So in your case /api or /api/ is the base URL path and the relative URL path css/all.css is resolved differently depending on the base URL path:
/api + css/all.css → /css/all.css
/api/ + css/all.css → /api/css/all.css
To conquer this you either need to
use absolute URL paths like /css/all.css that are independent from the base URL path
specify a different base URL in your HTML document (e.g. /); but note that this will affect all relative URLs and not just relative URL paths
adjust your relative URL paths to suite your base URL paths:
for /api use css/all.css
for /api/ use ../css/all.css
for /api/foo/bar/ use ../../../css/all.css, etc.
I guess the first solution is the easiest.

How can I get mod_rewrite to ignore certain directories and all their subdirectories?

I'm using Symphony CMS which as default has a mod_rewrite that rewrites all directories.
However, I need it to ignore the directories test and transfer and all their subdirectories.
Place a new .htacess access files in each of your two directories, and then place the following in:
mod_rewrite off
That should sort it.
Put this rule above the others:
RewriteRule ^(test|transfer)(/|$) - [L]

Resources