mod_rewrite in .htaccess - php

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]

Related

.htaccess rewrite for multiple variables

so I have a few different links on my website, and I am looking for some advice on how I can achieve this.
Some example links:
domain/index.php?page=commandCenter&ID=40
domain/index.php?page=view&action=edit&ID=40
domain/index.php?page=acpDashboard
I would like the rewrite to look like the following for each link:
domain/commandCenter/40
domain/view/edit/40
domain/acpDashboard
Though it seems in order for this to work, I need to have something in place for those variables.
The last link would not work, but
domain/acpDashboard/0/0
is what it needs to work.
Is this possible, and how would I go about it? I only have 3 different variables page, action, and ID.
Here is my htaccess atm!
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /index.php? page=$1&action=$2&ID=$3 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Thanks for any advice!
Your here is wrong you can reference .htaccess RewriteRule to preserve GET URL parameters this answer
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /index.php?page=$1&action=$2&ID=$3 [L]

htaccess rewrite not working for images

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.

Mod_rewrite so that /abc goes to index.php?name=abc

I need to have URLs fitting this criteria:
www.domain.com/abc
rewritten to be:
www.domain.com/index.php?name=abc
I can do it where the last letter(s) are fixed like this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*).htm$ index.php?name=$1
which means www.domain.com/abc.htm would go to www.domain.com/index.php?name=abc
but I need it to not have the .htm (or anything after the abc)
Can this be done? I've spent some time trying to find the solution but so far without success.
Thanks in anticipation.
Jon
Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) /index.php?name=$1 [L]
I usually use RewriteRule ^(.*)$ index.php?name=$1
I am not completely sure on your criteria though, this will send everything to the PHP script.
If you only want to rewrite the first part of a path ie. /abc but not abc/def or abc/ I can change it for you.

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]

How to set-up friendly URLs

I am looking for your recommandations on what would be the best way to implement friendly URLs.
What I currently do is redirect all 404 requests to folders or files that do not exist to index.php.
index.php reads the query string and makes a database call to see if the url is in the page_urls table then based on the page type fetches content etc etc.
The .htaccess contains the following lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Is there a more "clever" way of doing this please? Thank you.
Thank you.
The best way I've found is to do something like the following:
RewriteEngine on
RewriteRule ([a-zA-Z0-9_-]*)\.html index.php?page=$1 [L]

Categories