.htaccess RewriteRule help - .htaccess

I have been able to change the 'location' of my images with RewriteRule, but I have 3 lines of code to do this because of subdirectories in the image folders
RewriteRule ^images/([^/]+).(jpg|jpeg|bmp|gif|png)$ /includes/images/$1.$2
RewriteRule ^images/([^/]+)/([^/]+).(jpg|jpeg|bmp|gif|png)$ /includes/images/$1/$2.$3
RewriteRule ^images/([^/]+)/([^/]+)/([^/]+).(jpg|jpeg|bmp|gif|png)$ /includes/images/$1/$2/$3.$4
However, I would like to be able to not need to add to these if I ever add any deeper subdirectories.
I have tried many different approaches to this with no luck, and the following is what I am stuck with atm.
RewriteRule ^(images)(/?[^/])+(.jpg|.jpeg|.bmp|.gif|.png)$ /includes/$&
Does anyone have any ideas on how to get this code to work??
Also, is there any way to view the URL that is being used server-side?

Perhaps I'm missing something, but I don't see any reason that you couldn't simply do:
RewriteRule ^images/(.+)\.(jpg|jpeg|bmp|gif|png)$ /includes/images/$1.$2

Related

Internal rewrite to remove directory from url with htaccess

I've searched and tried many tutorials and problem solutions but couldn't achieve what I want.
If user open domain.com/xxx.php he see the content of domain.com/hosting/xxx.php
This
RewriteRule ^xxx.php$ /hosting/xxx.php
Works perfectly what i want to do, but there are over 150 pages in hosting directory and writing rules for each page does not seem right? any possible way to do it dynamically?
I tried this solution it works but it try to find those pages which are currently at root domain.com/xxx.php inside domain.com/hosting/
I search slimier questions have been asked many times before but non of the answer worked for me.
You need to also take care of reuests to the shortened, rewritten URL:
RewriteEngine on
RewriteRule ^/?hosting/(.*)$ /$1 [END,NC,R=302]
RewriteRule ^/?(.*)$ /hosting/$1 [END]

Make URLs pretty with one line of htaccess

So I've had this issue for some time and originally I just dealt with it the long way around but that didn't work so I was hoping for a more solid solution.
My problem currently is that using the following code:
RewriteRule ^error/([a-zA-Z0-9-]+)/$ error.php?id=$1
error.php?id=404 becomes this:
/error/404/
Which is perfect. However if I add a new page which uses an id at the end, I have to add it in to my htaccess which is extra work and doesn't seem very good in terms of best practice.
I was hoping with one line I could have all sorts of combinations but it doesn't work.
I tried the following which resulted in a default 300 multiple request error.
RewriteRule ^/([a-zA-Z0-9-]+)/$ $1.php?id=$2
Any help please?
You can use this
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/([^/]+)/$ /$1.php?id=$2 [L]
Example: /error/404/ will be rewritten to /error.php?id=404 if error.php exists
Note: maybe you'll have to disable MultiViews option (apache content negociation)
Options -MultiViews

rewriteRule for URL path not working correctly

I need help with a URL problem I've encountered with a rewriteRule.
What I need it to do is following: example.com/en/page/page/
At the moment the following works fine: example.com/en/page/
But once it goes like "example.com/en/page/page/" I receive a 404 - page not found error even if the page in fact is located in the serverfiles.
The clue here is that I use a variable in the /en/ part of the URL (multilanguage system) and it seems that I cannot figure out how to get it to work with that included.
At the moment I have the following rewriteRule in my .htaccess file.
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?lang=$1&uri=$2 [L]
Do any of you have a clue on what might work?
Best regards,
PureDarkness
You don't include anything behind the second /. You could try:
RewriteRule ^([^/]*)/(.*)$ index.php?lang=$1&uri=$2 [L]
And you can add [QSA] if you also need to get the parameters.

Trying to exclude css and js files with IIRF rewrite

I'm trying to get IIRF working so that it rewrites certain URLs, but ignores files like .css and .js files.
I have URLs like:
/admin/
/admin/user/
/admin/user/view-details/1
/admin/user/view-details/1?edit
Which all work, but I can't seem to get this to work:
/admin/_assets/css/admin.css
My .ini file looks like this:
RewriteRule /admin/([^/]+)/([^/]+)/([^/]+)\?edit$ /admin/index.php?edit&action=$1&sub-action=$2&id=$3 [L,I,QSA]
RewriteRule /admin/([^/]+)/([^/]+)/([^/]+)$ /admin/index.php?edit&action=$1&sub-action=$2&id=$3 [L,I,QSA]
RewriteRule /admin/([^/]+)/$ /admin/index.php?action=$1 [L,I,QSA]
RewriteRule ^/admin/(.*)$ - [L]
I've also tried:
RewriteRule (.+\.)(php|jpg|png|jpeg|gif|ttf|sql|txt|htm|zip|css)$ - [L]
The things I've tried give either a blank document when I navigate directly to the css file, or I get:
HTTP Error 404.4 - Not Found
The resource you are looking for does not have a handler associated with it.
I don't have much experience with IIRF or IIS, so I'm a bit stuck with what to try next. Am I looking in the wrong place? Or have I made a really basic mistake?
Thanks
Tip for debugging iirf: use StatusUrl to check first for problems.
I've managed to fix this by adding:
RewriteRule /admin/_assets/(.+)$ /admin/_assets/$1 [L,I,QSA]
to the top of my file. I'm still not sure why nothing is being logged, but this fixed it for my needs.

Rewrite rules for subfolders

This may seem like a silly question but I can't figure it out.
let's say I have a public_html folder with various folders like: Albatross, Blackbirds, Crows and Faqs.
I want to make it so that any traffic to Albatross/faqs.php, Blackbirds/faqs.php, Crows/faqs.php etc will see the file that is at faqs/faqs.php?bird=albatross or faqs/faqs.php?bird=crows or what have you.
If I go into the Albatross folder's .htaccess file I can do this
RewriteRule faqs.php$ /faqs/faqs.php?cat=albatross[QSA]
Which works fine, but I want to put something in the top level .htacces that works for all of them, so tried:
RewriteRule faqs.php$ /faqs/faqs.php?cat=albatross[QSA]
RewriteRule /(.*)/faqs.php$ /faqs/faqs.php?cat=$1 [QSA]
and even
RewriteRule /albatross/faqs.php$ /faqs/faqs.php?cat=albatross [QSA]
and various others but nothing seems to work, when I go to http://www.birdsandwhatnot.com/albatross/faqs.php I see the same file the same way it's always been. Does the presence of an .htaccess file in the subfolder conflict with the higher up .htaccess file?
Am I missing something?
A small correction should do the trick
RewriteEngine on
RewriteRule ^(.*)/faqs.php$ /faqs/faqs.php?cat=$1 [QSA]
"/" is not being passed to parser.
Hope it helps

Resources