Using multiple rewrite rules - .htaccess

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!

Related

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...

RewriteRule for 'pretty links' not working

I am trying to make a link that looks like https://www.exapmle.com/profile.php?u=8 to look like https://www.exapmle.com/profile/8
I have a tried variations of this in htaccess:
RewriteRule ^/profile/([0-9]+)/?$ profile.php?u=$1
RewriteRule ^/profile/([0-9]+)\.html /profile.php?u=$1
I don't know what i am doing wrong, the links don't change and I'm not getting any errors either
You may use this code in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /profile\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^/?profile/(\d+)(?:\.html)?/?$ profile.php?u=$1 [L,QSA,NC]

.htaccess rewriting GET

I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!
My current HTACCESS looks like this, however it doesnt do what I want.
RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L]
You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.
If you want people to type the php script in the URL bar you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/
Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}
For both ways and 301 redirect to pretty URL:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]

mod_rewrite rule for exact expression not working

I don't work to much with mod_rewrite so I could easily be just missing something simple. I want to match and replace a specific URL. The URL I want to rewrite would look like this:
http://www.sample.com/hello to http://www.sample.com/index.php?page=hello
My htaccess currently has the following rule in place:
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
However, when I implement the rule and I test it by trying to go to http://www.sample.com/hello or http://www.sample.com/hello/ I always get my 404 error document. ugh I feel like it should be simple and I must be missing something right there. Any help would be fantastic and much appreciated!
Here is my whole file for reference:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?sample\.com/ [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(gif|png) [NC]
RewriteRule ^(.*)\.(gif|jpe?g|png)$ sample.com/hotlink.png[R,NC,L]
RewriteCond %{HTTP_HOST} ^www.sample\.com$ [NC]
RewriteRule ^(.*)$ http://sample.com/$1 [R=301,L]
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
Thanks
Your code works perfeclty fine for me. Maybe you're missing
RewriteEngine On
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
or maybe mod_rewrite is not installed or loaded at all.
Look for
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
in your main apache configuration file httpd.conf
This is one of those times...
I cleared the htaccess file and only used the following couple lines and tested it out:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
#
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
I realized looking at the non custom 404 page that it referenced page.php and not index.php!
Not Found
The requested URL /page.php was not found on this server.
So turns out with all my edits I mistakenly typed page.php in the rule instead of index.php. I corrected it and the rule works. One of those times :)
Thanks all!

.htaccess - RewriteRule

Ok I have the following .htaccess and it works however I can seem to change it to the way I need it.
here is the current .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
Options +FollowSymlinks
I need to add the following
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&tpl=$2 [L]
So i am wondering how do I get that rule to work? as when I add it it does not.
So if you want to add a rule that will handle all requests except the first one that matches the RewriteCond And those would be split into /?
I think something like this would work. Please note that I changed the $1 to ${REQUEST_URI} in your rewrite condition
RewriteEngine on
RewriteCond ${REQUEST_URI} !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
RewriteRule ^([^/]*)/?(.*)?$ /index.php?cat=$1&tpl=$2 [L]
Options +FollowSymlinks

Resources