mod_rewrite image problems - image

I currently have this in .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^page/(.*)/(.*)$ /type1/internal.php?parentFolders=$1&pageTitleID=$2 [L]
When I visit http://localhost/type1/page/home/play it works fine, however I would like to take the /page/ out therefore being http://localhost/type1/home/play
I have tried the following:
RewriteRule ^(.*)/(.*)$ /type1/internal.php?parentFolders=$1&pageTitleID=$2 [L]
however some images seem to vanish, and some don't.
Also, I find hard links in my nav do not work either as they are asking for variables used in internal pages.
I am not sure if this is a absolute/relative problem as my CSS is written as
<link href="http://localhost/type1/global.css" rel="stylesheet" type="text/css" />
and links seem to be full links when I inspect them with my browser!?
Any help would be great

As I understand the .htaccess file is located in /type1/ subfolder. Try these rules:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /type1/internal.php?parentFolders=$1&pageTitleID=$2 [L,QSA]
The (.*) pattern you are using is too broad for first parameter, I use ([^/]+) instead (any character except slash).
Added 2 rewrite conditions .. so only requests to non-existing files and folders will be rewritten. This should help with disappeared images (or may not -- it all depends how how the links are written).
Added QSA flag to preserve any existing query string.

Related

htaccess mod_rewrite with trailing slashes issue

I was wondering if anyone could help? Basically I've got a set of mod_rewrite rules and they all work well except if I put a trailing slash at the end of the URL it goes crazy. Still show's the page (sometimes) except all of the CSS, JS, etc is not included because the path messes up when there's a trailing slash. The only way to solve it is to put a BASE tag which I would really like to avoid as it brings it's own set of issues. Is there something wrong with my htaccess? I just need it to ignore the trailing slashes.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^resetpassword/([^/.]+)/?$ resetPassword.php?resetCode=$1 [L]
RewriteRule ^activate/([^/.]+)/?$ activateAccount.php?ACTCode=$1 [L]
RewriteRule ^dashboard/?$ dashboard.php [NC,L]
RewriteRule ^login/?$ login.php [NC,L]
RewriteRule ^register/?$ register.php [NC,L]
RewriteRule ^messages/inbox/??$ messagesInbox.php [NC,L]
RewriteRule ^messages/archive/??$ messagesArchive.php [NC,L]
RewriteRule ^messages/??$ messagesInbox.php [NC,L]
There's likely not a good fix for this because you'll be switching "directory" levels in any case (like in messages/inbox).
The most common solution is to either use an absolute path to reference CSS, images etc.:
<link rel="stylesheet" href="/css/styles.css">
or to have a PHP variable containing the web root:
<link rel="stylesheet" href="<? echo $webroot; ?>/css/styles.css">
Both these approaches are vastly better than using a base tag, or doing some .htaccess-based redirection shenanigans.

CodeIgniter and specific rewrite rule

On my CodeIgniter site, I would like to add a specific rewrite rule, so that this url
http://www.exemple.com/cache.manifest
would rewrite to
http://www.exemple.com/controller/manifest
(because Safari 7 seems to only accept .manifest files for ApplicationCache)
So I try to add this line to my htaccess
RewriteRule ^cache.manifest$ controller/manifest
I added it before the other rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^cache.manifest$ controller/manifest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
But it returns a 404. If I change the line to
RewriteRule ^cache.manifest$ test.html
it works. So the first part on my rule is correct.
If I try to access directly to www.example.com/controller/manifest, it works to, so my url is correct.
I tried also
RewriteRule ^cache.manifest$ index.php/controller/manifest [L]
But it doesn't work either…
Any clue ?
Thanks a lot
I tried some tests on my local server and I think the following might work:
RewriteRule ^cache.manifest$ /index.php/controller/manifest [R,L]
I am not entirely sure if you need the leading "/" or "/index.php", so you
may need to experiment.
You need the [R] flag to force a redirect. In this situation, you want Apache
to look for the string cache.manifest in the URL, and then go to the CI page
controller/manifest.
It appears that you need to explicitly set the redirect.
Please let me know if this works. Good luck!

HTACCESS Rewrite seems to keep images and scripts from loading

I got this problem and I have been tyring alot (even deleting the htaccess) and I haven't found a solution.
A customer of my company has an application that I didn't make.... but I could nottice that there was an HTACCESS (and it happened to me before) and it seems to have some Rewrite rules. I am not familiar with HTaccess stuff, I am very ignorant about what's going on in this kind of files.
The page looks like this
The htaccess file contains this:
Options All -Indexes
Options +FollowSymLinks
RewriteEngine on
#4 variables
Rewriterule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ $1.php?$2=$3&$4=$5&$6=$7&$8=$9
#3 variables
Rewriterule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ $1.php?$2=$3&$4=$5&$6=$7
#2 variables
Rewriterule ^(.*)/(.*)/(.*)/(.*)/(.*)/$ $1.php?$2=$3&$4=$5
#1 variable
Rewriterule ^(.*)/(.*)/(.*)/$ $1.php?$2=$3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
Do you have any idea if that is the HTACCESS FAULT? If I delete the HTACCESS I get a 500 Internal Server Error
This sounds like a relative/absolute URI issue. The images/scripts/css are probably linked using relative URIs which is broken when you have a structure like this: (.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/, while it could work fine when you access the page like this: /$1.php?something.
So you either need to change all of the links so that they're absolute URLs (that start with a /) or create a relative URI base at the header of all of your pages:
<base href="/">

is there a way to see how a mod-rewrite acts in real-time?

I had a mod-rewrite rule that worked just fine for a few months:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
(this is located in site/folder).
This would redirect people from site/folder/something to site/folder/index.php?q=something, leaving the url nice and pretty.
Now, suddenly, while nobody touches the .htaccess file nor the code of the relevant pages, it has stopped working. While the urls stays the same as before, the content of the page is of site/index.php instead of (site/folder/index.php?q=something).
How can I debug this, what tools will allow me to follow the flow of the redirection rule?
In the server or vhost config, you can add these lines:
RewriteLog /tmp/rewrite.log
RewriteLogLevel 5
Then you can look at the output written to /tmp/rewrite.log (or wherever of your choosing) to see what mod_rewrite is doing in real-time. If you don't see any output, then the rules you have aren't being applied at all (possibly the override has changed or mod_rewrite isn't loaded?).

301 URL Forwarding with HTACCESS or PHP

Just curious if anyone can help me on this HTACCESS issue.
I have these OLD URLS that need to get forwarded properly.
Previous structure
domain.com/Canada/Accounting
domain.com/Canada/Trades
domain.com/Canada/Sales
Proper structure
CATEGORY - /jobs/accounting-jobs
LOCATION - /jobs/jobs-kelowna
TOGETHER - /jobs/accounting-jobs-kelowna
Domain Structure
domain.com/jobs/[category]-jobs-[location]
Is this possible, either by HTACCES or PHP...just don't want these 404'ed pages.
I have 86+ to do, if there is a good way to forward these.
This is what I have, but i'm unable to successfully forward the bad-urls properly.
OLD
/browse
/Toronto/
/Canada/Administrative
/Vancouver/
/Canada/Trades
/Calgary/
/Canada/Hospitality
This is my HTACCESS right now.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
#
# Trailing slash check
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
#
# PAGES
RewriteRule ^add-job/?$ /add-job.php [L]
RewriteRule ^jobs/?$ /results.php [L]
RewriteRule ^sitemap/?$ /sitemap.php [L]
#
# SEARCH
# CATEGORY - accounting-jobs
# LOCATION - jobs-kelowna
# TOGETHER - accounting-jobs-kelowna
RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)?$ results.php?whatwhere=$1&page=$2
RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)/?$ results.php?whatwhere=$1&page=$2
To 301 redirect your pages you can do something like:
RewriteEngine On
RewriteBase /
RewriteRule ^(\w+)/(\w+)$ /jobs/$2-jobs-$1 [R=301,L]
This only addresses the urls from your previous structure (the combinations, you have not shown any previous urls with just location or category) but note that Canada will stay Canada, it does not become canada. You can change everything to lower case using rewrite as well.
You also have to take care that you don't rewrite any of the current urls but without more information, this should do it.
Edit: For the location-only urls you could use a rule like:
RewriteRule ^(\w+)/$ /jobs/jobs-$1 [R=301,L]
Again, you need to look out that your rewrite rule does not interfere with your current urls. If that is the case, you would need to redirect every old url manually.
For lower-case new urls, you should search SO, there are some questions with good answers about converting a mized-case variable to lower-case.
If you have mod_rewrite, you can add these lines to your .htaccess file:
RewriteEngine on
RewriteRule ^Canada/Accounting$ /jobs/accounting-jobs [R,L]
However, it's not clear from your question exactly what you want mapped. Are the 3 previous URLs supposed to redirect to the 3 new ones? They don't seem to be equivalent.

Resources