Adding a third Mod Rewrite - mod_rewrite - php

guys. I'm trying to add a third rewrite condition in my .htaccess. The problem is, I have no idea how to add another one. So if anyone has a good in-depth tutorial on it I would be greatful.
Anyway back to business. Here is my current code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users.php?name=$1 [QSA,L]
The above as you might see removes the .php extensions and makes all files that don't exist go to users(IE: website.com/user goes to user's profile page).
So now I need to add one for the forum. I would like it rewrite like "website.com/Group/Category/Thread".
Is this even possible, or do I need to lower my expectations for this? Any help would be greatly appreciated. Thanks :D

Hmm I remember posting this answer :)
Anyway you can create a new rule on top of previous rules like this (and replace /Group/Category/Thread with whatever you have) :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^forum/?$ /Group/Category/Thread [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users.php?name=$1 [QSA,L]

You can insert more rules. Given that you want these rules to be matched before your final catch-all to user.php, you should place them before that final rule.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
# match forum, forum/group, forum/group/category and forum/group/category/thread urls
RewriteRule ^forum/?$ forum.php?group=$1&category=$2&thread=$3 [L]
RewriteRule ^forum/([^/]+)/?$ forum.php?group=$1 [L]
RewriteRule ^forum/([^/]+)/([^/]+)/?$ forum.php?group=$1&category=$2 [L]
RewriteRule ^forum/([^/]+)/([^/]+)/([^/]+)/?$ forum.php?group=$1&category=$2&thread=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users.php?name=$1 [QSA,L]

Related

Rewrite Rule for id .htaccess

Is there any possibility to rewrite url's with get variables globally, instead of specific files?
Changing
http://example.com/{something}?id=1
To:
http://example.com/{something}/id/1
Or without the word id:
http://example.com/{something}/1
This is what I got so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.phtml
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.phtml
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
With this I could remove the .phtml extension
I hope there is a specific answer for this!
I am assuming something to be a php file,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/id/([\d]+)$ $1.php?id=$2 [L] #with id in between
or
RewriteRule ^([\w-]+)/([\d]+)$ $1.php?id=$2 [L] #without id in between
You have to make sure rules are not conflicting.

htaccess rewrite rule for subfolder (nice url)

I have trying to figure out how to get working in subfolder "admin" the same thing which works in root dir, problem is I don't know how to do that.
Currently I have:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
# /
RewriteCond %{REQUEST_URI} !\.[[:alnum:]]+$
RewriteRule ^(.+[^/])$ /$1/ [R=301]
Now it works like:
http://example.com/page translate into http://example.com/?arg=page
What I would like to do in addition is this:
http://example.com/admin/page translate into http://example.com/admin/?arg=page
I don't know at all how to make that happen, can somebody help me please?
Thanks
You can use these rules with appropriate RewriteBase:
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
Or just add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin/(.*)$ admin/index.php?arg=$1 [L,QSA]

Not Found when shortening URL

So I have shortened article.php?id=10 to article/10. And it all seemed to work fine. But little did I know that it ruined the rest of my URLs. So with http://localhost/forgot/, I'd have to go to http://localhost/forgot/index to actually reach it. Here's what I'm using
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I want to go to http://localhost/forgot/ instead of http://localhost/forgot/index/ Any ideas?
Have your rules like this:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1 [L,QSA,NC]
# rewrite from /dir/file/ to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

.htaccess redirection based on number of slashes(/) in url

This is my first htaccess file.
this is what i have written
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif|otf|eot|svg|ttf|woff|jpeg)$ - [L]
RewriteRule ^([^/]*)/([^/]*)$ maincategory/index.php?main=$1&sub=$2 [L,QSA,NC]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ subcategory/index.php?main=$1&sub=$2&cat=$3 [L,QSA,NC]
The problem what I am facing is both are pointing to maincategory.
How can I redirect based on number of "/" in the URL?
Thanks in advance.
Rearrange the rules and try it this way. Let me know if that works for you.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif|otf|eot|svg|ttf|woff|jpeg)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ subcategory/index.php?main=$1&sub=$2&cat=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ maincategory/index.php?main=$1&sub=$2 [L,QSA,NC]

Php multiple mod rewrite rule conflict

I'm trying clean my url from
localhost/xxx/profile.php?name=google to
localhost/xxx/google
Below is my htaccess file
RewriteEngine On
RewriteBase /creatorsink/
RewriteRule ^([a-z]+)/?$ profile.php?name=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
If i remove RewriteRule ^(.*)$ $1.php [L] it's working properly, if i do so then url not opening without .php extension
Is there any way to use both without conflicting each other.
Ordering of rule is causing problem. Your first rule is grabbing every URI. Reverse the order and make your first rule little more smart.
RewriteEngine On
RewriteBase /creatorsink/
RewriteRule ^([^/]+)/?$ profile.php?name=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/creatorsink/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Categories