RewriteRule to accept 2 parameters - .htaccess

mydomain.com/MyFolder/parameter-1
I have this htaccess RewriteRule -
RewriteRule ^([a-z0-9\-]+)/?$ index.php?c=$1 [NC,L]
The .htaccess file is inside MyFolder and this only accept a single parameter.
How can do a RewriteRule to accept 2 parameters
mydomain.com/MyFolder/parameter-1/parameter-2
Thanks

use this:
RewriteEngine On
# mydomain.com/MyFolder/parameter-1
RewriteRule ^([a-z0-9\-]+)/?$ index.php?c=$1 [NC,L]
# mydomain.com/MyFolder/parameter-1/parameter-2
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ index.php?c=$1&d=$2 [NC,L]

Related

.htaccess detect "/logged-in" at end of any URL and redirect to same URL

I'd like to know how to create a .htaccess rewrite rule that can check for "/logged-in" at the end of any pretty URL, and then pass a GET parameter back to that same page.
Essentially the user can log in on any page, then the page reloads with /logged-in attached to it. I need to run some PHP that checks:
if ($_GET['mode'] == 'logged-in')
to display a modal on the page they were on confirming the login.
It can be any page on the site, any level deep.
So an example URL might be:
www.domain.com/shop/category-id/logged-in
which would need to rewrite to:
www.domain.com/shop/category-id/?mode=logged-in
Obviously the above URL is already being rewritten prior to this as it is actually loading:
www.domain.com/shop/category.php?id=category-id
So essentially need to know how to detect:
*/logged-in
and get back to
*/?mode=logged-in
while keeping the URL pretty.
Here are the current rewrite rules. ALL of these need to also support placing /logged-in at the end of them, and I was hoping I wouldn't have to manually make a new rewrite rule for each one.
# BLOG
RewriteRule ^blog/page/([0-9]+)/?$ blog.php?page=$1 [NC,L]
RewriteRule ^blog/([A-Za-z0-9-_]+)/?$ blog.php?cat=$1 [NC,L]
RewriteRule ^blog/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ blog-post.php?cat=$1&url=$2 [NC,L]
# SHOP API
RewriteRule ^shop/cart/add/([0-9]+)/([0-9]+)?/?$ shop/?mode=add2cart&id=$1&sku=$2 [NC,L]
RewriteRule ^shop/cart/update/([0-9]+)/([0-9]+)/([0-9]+)/?$ shop/?mode=updatecartitemcount&id=$1&sku=$2&count=$3 [NC,L]
RewriteRule ^shop/cart/remove/([0-9]+)/([0-9]+)/?$ shop/?mode=updatecartitemcount&id=$1&sku=$2&count=0 [NC,L]
RewriteRule ^shop/(generate_token)/?$ shop/?mode=$1 [NC,L]
RewriteRule ^shop/(get_details)/([0-9]+)/?$ shop/?mode=$1&id=$2 [NC,L]
# SHOP
RewriteRule ^shop/([A-Za-z0-9-_]+)/?$ shop/category.php?cat=$1 [NC,L,QSA]
RewriteRule ^shop/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ shop/category.php?cat=$1&subcat=$2 [NC,L,QSA]
RewriteRule ^shop/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([0-9-_]+)?/?$ shop/product.php?cat=$1&url=$2&id=$3 [NC,L,QSA]
RewriteRule ^policies/([A-Za-z0-9-_]+)/?$ policy.php?url=$1 [NC,L]
# CHECKOUT
RewriteRule ^checkout/(review)?/?$ checkout/index.php?view=review [NC,L]
RewriteRule ^checkout/(checkout|payment|complete)/?$ checkout/index.php?view=$1 [NC,L]
RewriteRule ^checkout/load/([A-Za-z0-9-_]+)/?$ checkout/index.php?mode=load_content&view=$1 [NC,L]
RewriteRule ^checkout/(redeem_coupon|get_total|generate_token)/?$ checkout/index.php?mode=$1 [NC,L]
RewriteRule ^checkout/update_shipping/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ checkout/index.php?mode=update_shipping&view=$1&country=$2&shipping_type=$3 [NC,L]
# USER ACCOUNTS
RewriteRule ^account/order/([0-9-_]+)/?$ view-order.php?id=$1 [NC,L]
RewriteRule ^account/order/([0-9-_]+)/print-invoice/?$ view-order.php?id=$1&mode=print-invoice [NC,L]
RewriteRule ^account/update/([A-Za-z0-9-_]+)/?$ account.php?mode=update&section=$1 [NC,L]
RewriteRule ^forgot-my-password/submit/?$ forgot-my-password.php?mode=submit [NC,L]
RewriteRule ^update-password/([A-Za-z0-9-_]+)/?$ forgot-my-password.php?hash=$1 [NC,L]
RewriteRule ^update-password/([A-Za-z0-9-_]+)/submit/?$ forgot-my-password.php?mode=reset-password&hash=$1 [NC,L]
RewriteRule ^register/submit/?$ index.php?mode=register [NC,L]
RewriteRule ^login/submit/?$ index.php?mode=login [NC,L]
RewriteRule ^logout/?$ index.php?mode=logout [NC,L]
RewriteRule ^logged-out/?$ index.php?mode=logged-out [NC,L]
RewriteRule ^subscribe/?$ index.php?mode=newsletter-subscribe [NC,L]
RewriteRule ^confirm/([A-Za-z0-9-_]+)/?$ index.php?mode=newsletter-confirm&code=$1 [NC,L]
RewriteRule ^confirm-account/([A-Za-z0-9-_]+)/?$ index.php?mode=account-confirm&code=$1 [NC,L]
RewriteRule ^contact/submit/?$ contact.php?mode=submit [NC,L]
# SEARCH RESULTS
RewriteRule ^search/?([A-Za-z0-9-_+]+)?/?$ search-results.php?query=$1 [NC,L]
RewriteRule ^search/([A-Za-z0-9-_+]+)/(render_results)/?$ search-results.php?query=$1&mode=$2 [NC,L]
Scott
You can use this rule just below RewriteEngine On line:
RewriteEngine On
RewriteRule ^(.+)/(logged-in)/?$ $1?mode=$2 [L,QSA,NC]
# all your rules go below this line

Regex .htaccess with UTF-8

i've got .htaccess file:
RewriteEngine on
RewriteRule ^category?$ category.php
RewriteRule ^category/([0-9a-zA-Z]+) category.php?name=$1
Need to even use letters "ě,š,č,ř,ž,ý,á,í,é" etc..
How to do that?
You can use:
RewriteEngine on
RewriteRule ^category?$ category.php [NC,L]
RewriteRule ^category/([^/]+)$ category.php?name=$1 [NC,L]
Which accepts all characters

define variable for directory in htaccess and use it

Lets think that I have such code in htaccess:
RewriteRule ^admin/registration/?$ admin/qeyd.php [NC,L]
RewriteRule ^admin/login/?$ admin/login.php [NC,L]
RewriteRule ^admin/profile/?$ admin/profile.php [NC,L]
But I want to define a variable for "admin" and write it like this:
admin_directory_variable = custom_name
RewriteRule ^custom_name/registration/?$ admin/qeyd.php [NC,L]
RewriteRule ^custom_name/login/?$ admin/login.php [NC,L]
RewriteRule ^custom_name/profile/?$ admin/profile.php [NC,L]
How can I do it ?
Create admin/.htaccess file and use these rules:
RewriteEngine On
RewriteBase /admin/
RewriteRule ^registration/?$ qeyd.php [NC,L]
RewriteRule ^(login|profile)/?$ $1.php [NC,L]
Apache 2.4 has the Define keyword which you use like this:
Define name value
and use like this:
DocumentRoot ${name}
Unfortunately, it can't be used in .htaccess files. See Apache 2.4 documentation

htaccess file to remove folder, and replace underscores with dashes

I need to change the following url
http://somedomain.com/news/a_sample_news_article.html
to
http://somedomain.com/post/a-sample-news-article
I have this in my htaccess which works, but I am sure it can be improved upon - does anyone have a better solution?
RewriteEngine on
# replace underscores
RewriteRule ^(news)/([^_]*)_+(.*)$ /$1/$2-$3 [L,NC,R=302]
# redirect the directory from news to post
RewriteRule ^news/(.*)$ /post/$1 [R,L]
# remove .html from end of url
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
Any help much appreciated!
To redirect
/news/foo_bar
to
/post/foo-bar
you can use the following rule :
RewriteEngine on
# redirect "/news/foo_bar" to "/foo_bar"
RewriteRule ^news/(.+)$ /$1 [L,R]
#2 replace underscore with hypens
RewriteRule (.*)_(.*) $1-$2 [N,E=uscores:yes]
RewriteCond %{ENV:uscores} yes
RewriteRule ^(.+)$ /post/$1 [L,R]

htaccess one OR two parameters

I want a URL like this:
www.mywebsite.com/user
So I do:
RewriteRule ^([a-zA-Z0-9_/-]+)$ profile.php?user=$1
it is working.
but SOMETIMES I need a secound parameter, like:
www.mywebsite.com/user/22 = user?id=22
or I need a secound parameter like this:
www.mywebsite.com/user/images = user?pg=images
notice that I can have an user/id or user/pg for different actions.
Any ideas how can I do this?
You can have 3 rules like this:
Options -MultiViews
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# to handle /user/22
RewriteRule ^user/(\d+)/?$ user.php?id=$1 [NC,L,QSA]
# to handle /user/images
RewriteRule ^user/(\w+)/?$ user.php?pg=$1 [NC,L,QSA]
# existing rule
RewriteRule ^([\w-]+)/?$ profile.php?user=$1 [L,QSA]
It's very easy:
RewriteRule ^user/([0-9]+)$ index.php?user=$1
RewriteRule ^user/([a-zA-Z0-9_/-]+)$ index.php?pg=$1

Resources