htaccess rewrite not working for images - php

Appraciate any help anyone can give with this,
I have an old url structure of: products/category(category is dynamic and only used as an example) and i'm trying to change to category/products, to do this I used
RewriteRule ^(.*)/products index.php?category=$1
This is working however problem is my images are stored as images/products/image and the rule is writing all my image links,
I've tried:
RewriteRule ^images/products/(.*) images/products/$1
just to rewrite images but it doesn't seam to be working
my htaccess file looks like this:
RewriteRule ^images/products/(.*) images/products/$1
RewriteRule ^(.*)/products index.php?category=$1
Thanks in advance,
Dan

The easiest would be to add a rule to not rewrite any paths of existing directories and files (your images...).
You can do that by adding these rules before your rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/products index.php?category=$1

Jeroen's answer works as well, but you can also specify a rule as an end point
RewriteRule ^images/products/(.*) images/products/$1 [L]
RewriteRule ^(.*)/products index.php?category=$1
The '[L]' should prevent the second rule from being parsed as the first rule should work as expected.

Related

How rewrite TWO URLS for .HTACCESS?

One pages of site have such adds: domain.com/posts/name.php
Other pages of site have such adds: domain.com/pages/name.php
I need to cut from these adds posts/ and pages/ only.
Firstly, I tried to use in .htaccess next rules:
RewriteRule ^([A-z0-9-]+)$ /posts/$1
RewriteRule ^([A-z0-9-]+)$ /pages/$1
Don't help.
Secondly, I tried to use in .htaccess such rules and conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ posts/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ pages/$1 [L]
As result: image of mistake 404
Also I tried to use such method:
RewriteCond %{REQUEST_URI} ^([A-z0-9-]+)$/(posts|pages)/([A-z0-9-]+)/$1 [NC]
RewriteRule .* domain.com/%1%3
Don't help....
what i am understanding you need to remove the post and pages from the url following code will help you for that.
You would need the rewrite module of Apache: mod_rewrite.
Then do something like this:
RewriteEngine on
RewriteRule ^post/(.*)$ $1
RewriteRule ^pages/(.*)$ $1
Here is the official documentation of mod_rewrite: click
please test it onece I dint test the code but did same for replacing url and its work for me.
Thanks.
The solution of this problem such:
We enter all information about the page in the MYSQL database:
image of database
We generate a page from the database:
generation of page (php)
Set the generated page for Human-Friendly Url:
file .htaccess
So everything works, and POSTS there are in their folder in the database, and PAGES in their folder.
folder in database
All is clean. If you can do better somewhere, then correct me, please.

Rewriting in .htaccess with forward slashes

here is what I need. I have the url pearlsquirrel.com/profilecomments.php?u=eggo.
eggo being my username and the part of the dynamic url that changes. I would like to rewrite the url to say pearlsquirrel.com/eggo/comments, using .htaccess.
Here is what I have so far:
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ profilecomments.php?u=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profilecomments.php?u=$1
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel\.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1/comments [L,R=301]
but I just can not get it to work. Any help would be greatly appreciated. Thanks!
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/comments$ profilecomments.php?u=$1 [L]
NOTE
If you've used relative paths for your images, stylesheets etc. you need to change those into absolute paths or relative paths using the servers root folder as base in order to get your site to display properly.
For example, it will think that images/image.png is /eggo/comments/images/image.png
But, if you instead add a preceding slash /images/image.png your file paths will always start from the servers root folder and your site won't get messed up when you're rewriting your URL's.
Your first rule, overrides all the rest.
What you are describing (if I understood correctly), you need to handle /user/comments by profilecomments.php?u=user
RewriteRule ^([a-zA-Z0-9_-]+)/comments profilecomments.php?u=$1 [L]
this should do it.

mod_rewrite for multiple requests not working

Perhaps someone can help me with this before I go completely crazy with it. I have a site with two urls which I want to redirect.
I want all traffic except the gallery pages to go to index.php.
This is my htaccess:
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.*)$ /index.php?/$1
RewriteRule ^gallery(/((([a-zA-Z0-9-]+)(/(\d+))?)/?)?)?$ gallery.php?groupId=$4&showpage=$6 [NC,QSA,L]
This works except for one part. The rewrite rule for the gallery is not fully working. It is sending through the groupId to the gallery.php script but I am not getting through the showpage argument. In fact when the showpage is included in the url I get a 404. So for instance.
These work and are handled correctly by the rewrite rule
gallery
gallery/
gallery/mygroup
gallery/mygroup/
This does not work and throws 404's.
gallery/mygroup/2
Nothing I do seems to fix this and I would appreciate your help on this. If I set the showpage. The gallery script works if I feed it the old
gallery.php?groupId=mygroup&showpage=2
so I am sure the htaccess rule is not catching the url.
Thanks in advance.
Your regex seems a little over-the-top. Use two simpler rewrite rules instead:
RewriteRule ^gallery/([a-zA-Z0-9-]+)/?$ gallery.php?groupId=$1 [NC,QSA,L]
RewriteRule ^gallery/([a-zA-Z0-9-]+)/(\d+)/?$ gallery.php?groupId=$1&showpage=$2 [NC,QSA,L]

mod_rewrite changing /subpage/ to /subpage

Okey, so this is my problem.
I want to use mod_rewrite to make nice looking urls for my site.
I want them all to have good looking url like www.mypage/tennis or www.mypage/soccer instead of www.mypage/?page=tennis and www.mypage/?page=soccer
And with the following rules i can achive this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* index.php [L]
So now if I type in www.mypage/soccer my PHP script reads this url and does it's magic translating this to $_GET['page'] = soccer. All this works fine!
Problem is if I would type the URL www.mypage/soccer/ all of a sudden every linked css or image cannot be found by the website, since it now looking in the none existing folder /soccer/ off course.
How do I make a rewrite rule that transforms /soccer/ to /soccer or any other /blabla/ to /blabla
Hope my question is clear! Also if anyone have any good pages where I can learn more regular expressions i would be very happy!
Try this:
RewriteRule ^([A-Za-z0-9-]+)/?$ ?page=$1 [L]
I think you should not use mod_rewrite for this, but rather fix your CSS and image paths.
Change the paths from relative to absolute,
meaning, the paths should begin with a /.
If you have this, no matter on which site your are, /images/myimage.png will always refer to www.mypage.com/images/myimage.png.
# For URIs with query string:
RewriteCond %{QUERY_STRING} (.+)
RewriteRule (.*)/$ ?page=$1&%1 [L]
# All other
RewriteRule (.*)/$ ?page=$1 [L]

mod_rewrite in .htaccess

Hey all, how can I reduce this:
www.example.com/index.php?page=viewblog&category=awesome
down to this:
www.example.com/blog/awesome
The above lists all of the blog posts in that category, but I also want scope for adding the title of the post on the end of it as well, like this:
www.example.com/index.php?page=viewblog&category=awesome&post=why-trees-are-green
And this needs to shorten down to:
www.example.com/blog/awesome/why-trees-are-green
Any ideas, anyone? Thanks in advance :)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule blog/([^/]+)/?$ index.php?page=viewblog&category=$1 [L]
RewriteRule blog/([^/]+)/([^/]+)/?$ index.php?page=viewblog&category=$1&post=$2 [L]
The condition makes sure the requested URL is not an actual file, which you want to serve (css, images, etc.)
Then, one rule per level, the first one will rewrite
http://yourserver.com/blog/whatever to
http://yourserver.com/index.php?page=viewblog&category=whatever
The second one will rewrite http://yourserver.com/blog/whatever/whenever to
http://yourserver.com/index.php?page=viewblog&category=whatever&post=whenever
If you need more levels, add more rules accordingly.
Something like this?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/([^./]+)$ index.php?page=view$1&category=$2 [L]
RewriteRule ^([^./]+)/([^./]+)/([^./]+)$ index.php?page=view$1&category=$2&post=$3 [L]

Categories