url rewriting using .htaccess for different urls - .htaccess

I'm trying to create clean urls using .htaccess on my website i have this
RewriteEngine on
RewriteRule ^([^/]*)/$ /full_posts.php?permalink=$1 [L]
RewriteRule ^category/([^/]*)/$ /search.php?category=$1 [L]
the first one is working correctly but the second one is not working, I'm writing both one after another. please help!

The rules you are looking for are:
RewriteRule ^([^/]*)/?$ /full_posts.php?permalink=$1 [L]
RewriteRule ^category/([^/]*)/?$ /search.php?category=$1 [L]
I have made the trailing slash optoinal in both the cases.
You can test your rules on:
http://htaccess.madewithlove.be/

Related

Using multiple rewrite rules

i'm trying to create multiple rewrite rules to make friendly URL but what i did, makes my website throw error 500.
I've tried this but can't seem to make it work.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*) /index.php?category=$1 [L]
RewriteRule ^(.*)/(.*) /index.php?category=$1&subcategory=$2 [L]
RewriteRule ^(.*)/(.*)/(.*) /index.php?category=$1&subcategory=$2&userid=$3 [L]
What i need is basically to make this urls work:
domain.com/GetAnnouncements as domain.com/index.php?category=GetAnnouncements
domain.com/Persona/GetAchievements/2 as domain.com/index.php?category=Persona&subcategory=GetAchievements&userid=2
and there also should be third option that works 'in between' without 3rd parameter which is &userid=2
With your shown samples please try following .htaccess rules file.
Make sure to use either 1st OR 2nd solution only at a time.
Please make sure:
To keep your .htaccess rules file, index.php file in your root location.
Clear your browser cache before testing your URLs.
1st solution: Generic rules where using regex.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)/?$ /index.php?category=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/?$ /index.php?category=$1&subcategory=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/(/d+)/?$ /index.php?category=$1&subcategory=$2&userid=$3 [L]
OR 2nd solution: Using specific string/URLs only as per your shown samples.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(GetAnnouncements)/?$ /index.php?category=$1 [NC,L]
RewriteRule ^(Persona)/(GetAchievements/)/?$ /index.php?category=$1&subcategory=$2 [NC,L]
RewriteRule ^(Persona)/(GetAchievements/)(/d+)/?$ /index.php?category=$1&subcategory=$2&userid=$3 [NC,L]
After some more googling and consulting with my friend we came to this solution which works:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*)$ index.php?category=$1&subcategory=$2&userid=$3 [L,QSA]
RewriteRule ^(.*)/(.*)$ index.php?category=$1&subcategory=$2 [L,QSA]
RewriteRule ^(.*)$ index.php?category=$1 [L,QSA]
Thank you everyone who tried to help!

htaccess RewriteRules Strange behavior

Soo i have this htaccess file in my subfolder for a project:
RewriteEngine On
# HTTP to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R]
RewriteRule ^homepage$ index.php [L]
RewriteRule ^news$ news.php [L]
RewriteRule ^store$ store.php [L]
RewriteRule ^staff$ teams.php?t=1 [L]
In this file "homepage" works, "news" works and "staff" too but "store" not
I had a similar problem last week but i abandoned it becouse i tried everything:
RewriteRule ^/?news/(.*)$ news.php?art=$1 [L]
Here i want a rewrite rule /news/foobar to /news.php?art=foobar
I know i maybe not understand htaccess fully but if some work why others not?
It can be usefull information i use LiteSpeed Webserver
Soo this is just an answer who has this same problem when you use cyberpanel/openlitespeed you need to restart lsws everytime when you modify your .htaccess only then it will work.
If you use OLS Enterprise you dont need to do this...

.htaccess file, RewriteRule is working partially

I am trying to write some rewrite rules in .htaccess file in order to redirect the old pages of the website to the new php pages. Two of the rules are not working, any idea?
The rules are similar, they are trying to redirect for example from
www.prova.com/YC/13123.html to www.prova.com/YC/YC.php?capitolo=13123
the rules are working except for "YC" and "thegamer" (there is a comment in the code added to explain which one).
RewriteRule ^.*YC/(.*)\.(html)$ YC/YC.php?capitolo=$1 [R=301] <-- not working
RewriteRule ^.*RS/(.*)\.(html)$ RS/RS.php?capitolo=$1 [R=301]
RewriteRule ^.*BB/(.*)\.(html)$ BB/BB.php?capitolo=$1 [R=301]
RewriteRule ^.*thegamer/(.*)\.(html)$ thegamer/Gamer.php?capitolo=$1 [R=301] <-- not working
RewriteRule ^.*CB/(.*)\.(html)$ CB/CB.php?capitolo=$1 [R=301]
RewriteRule ^.*Hellper/(.*)\.(html)$ Hellper/Hellper.php?capitolo=$1 [R=301]
RewriteRule ^.*Dice/(.*)\.(html)$ Dice/Dice.php?capitolo=$1 [R=301]
RewriteRule ^.*Lars/(.*)\.(html)$ Lars/Lars.php?capitolo=$1 [R=301]
Problem Solved, for mistake i uploaded another .htaccess in the 2 folder that were not working :)

rewritecond in .htaccess using patterm matching

I'm trying to put together a rewrite condition in to match the last section of a URL that ends in 10 alphnumeric characters like:
http://www.someurl.com/EHN4K5LFWQ
and route it to:
http://www.someurl.com/index.php/EHN4K5LFWQ
I've put together the following so far but I can't seem to get it to work after struggling for a couple days using a number of variations both with and without a rewrite rule.
RewriteCond %{REQUEST_URI} ^dir1/dir2/([A-Z,0-9]{10})$
RewriteRule ^(.*)$ dir1/dir2/index.php/$1 [L]
Any help would be greatly appreciated.
If the htaccess file is in your document root, you can simply use this rule:
RewriteRule ^dir1/dir2/([A-Z,0-9]{10})$ /dir1/dir2/index.php/$1 [L]
If the htaccess file is in the dir2 folder, then you can use this rule:
RewriteRule ^([A-Z,0-9]{10})$ index.php/$1 [L]

.htaccess Rewrite for clean URLs w/ 2 get variables

My current .htacess looks like this:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-]+)/$ index.php?page=$1
which allows me to take a a link such as
http://www/myserver.com/somepage
and rewrite it into
http://www.myserver.com/?page=somepage.
but how would i make it go deeper? I want to be able to do:
http://www.myserver.com/somepage/subpage
and turn it into
http://www.myserver.com/?page=somepage&subpage=subpage
Thanks
Add the following rules with additional capturing group for the second parameter
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)$ index.php?page=$1&subpage=$2
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/$ index.php?page=$1&subpage=$2
If you have the potential to catch links that do not have subpages, then you'd want to catch the subpages with the first rule. Then you can catch the rest and send to page.
RewriteEngine On
# catch potential subpages first
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/?$ index.php?page=$1&subpage=$2 [L]
#combines your original rule to allow for optional end slash
RewriteRule ^([a-zA-Z0-9-z\-]+)/?$ index.php?page=$1 [L]
You could combine them into one rule, but it would potentially generate queries with an empty subpage. I prefer the cleaner internal queries, but for arguments sake:
RewriteEngine On
# One rule to rule them all
RewriteRule ^([a-zA-Z0-9-z\-]+)(/([a-zA-Z0-9-z\-]+))?/?$ index.php?page=$1&subpage=$3 [L]
Edit: Throwing in this link to a rewrite tester. Very useful. Although, when I tested my rules, it brought up an issue with your original [] grouping. What is the -z- at the end for?
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1&id=$2
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1&id=$2
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1&id=$2&value=$3
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1&id=$2&value=$3

Resources