RewriteRule required for URL's with 2 query parameters - regex

theyouthtalent.com/user_wall.php?User=khalidmirza&Talent=Business
I want to rewrite and redirect above mentioned URL like
http://theyouthtalent.com/Business/khalidmirza
where Business >> Talent=Business and khalidmirza >> User=khalidmirza
right now I am using these below mentinoed rules which are fine for
http://theyouthtalent.com/khalidmirza
but I want to include Talent (Business) before User (khalidmirza)
Kindly propose change in below rules to get desire result.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)$ user_wall.php?User=$1&Talent=$2 [L]
RewriteCond %{THE_REQUEST} user_wall\.php
RewriteCond %{QUERY_STRING} User=(\w+)&Talent=(\w+)
RewriteRule ^user_wall\.php$ /%1? [R=301,L]

Your regex to capture 2 components isn't correct in the rewrite rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /user_wall\.php [NC]
RewriteCond %{QUERY_STRING} ^User=(\w+)&Talent=(\w+)$ [NC]
RewriteRule ^user_wall\.php$ /%2/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/(\w+)/?$ user_wall.php?User=$2&Talent=$1 [L,QSA]

Related

Modify code htaccess to accept parameters via URl

Modify code to accept parameters via URl. Rules to redirect URLs to www.site.com/acessorios.php tried are as follows, where .htaccess rules file is present along side with app folder.
RewriteEngine ON
RewriteBase /app/views/
##External redirect rules from here.
RewriteCond %{THE_REQUEST} \s/app/views/([^.]*\.php)\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules from here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]*\.php)/?$ /app/views/$1 [QSA,L]
Samples:
www.site.com/accessories.php?id=1
www.site.com/accessories.php?id=1&name=Tiago
www.site.com/accessories.php?id=1&name=Tiago&image=foto.png
Samples:
www.site.com/accessories/1
www.site.com/accessories/1/Tiago
www.site.com/accessories/1/Tiago/foto.png
This order may be different
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /app/views/
##External redirect rules from here....
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRUle ^ /%1/%2/%3? [R=301,L]
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)&name=(\S+)&image=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
##Internal rules from here onwards....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2&name=$3&image=$4 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2&name=$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2 [QSA,L]

Remove the query string from url using htaccess

I need some help with htaccess. Would you be so kind to assist a little bit?
I have a URL like this
https://example.com/index.php?p=application-intelligence
[or]
https://example.com/?p=application-intelligence
Basically the index.php is passed some parameter 'home' to know which page to load i.e. home.php
So I've tried to follow your post on your blog but with not much luck.
So I'd like the final code to be
https://example.com/application-intelligence
Here's my code.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1
Also for the
https://example.com?p=home
I'd like it to be just
https://example.com
You may use these rules:
RewriteEngine on
# handle /?p=home
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?p=home\s [NC]
RewriteRule ^ /? [R=301,L,NE]
# handle /?p=<something>
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
This is what i have in my apache virtual host conf file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

.htaccess rewrite rules for multiple sites

Scenario
I have 3 domain names:
www.hellokitty.com
www.keroro.com
www.doraemon.com
I want to host all of them in one web hosting with the following directories:
./htdocs/hellokitty.com/
./htdocs/keroro.com/
./htdocs/doraemon.com/
All of them are linked to the same hosting server with A record.
However, my hosting do not support virtual host.
I need .htaccess files to rewrite the URLs
So, I tried in this way:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?hellokitty.com$ [NC]
RewriteCond %{REQUEST_URI} !^/hellokitty\.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /hellokitty\.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?hellokitty.com$ [NC]
RewriteRule ^(/)?$ hellokitty.com/index.php [L]
RewriteCond %{HTTP_HOST} ^(www.)?keroro.com$ [NC]
RewriteCond %{REQUEST_URI} !^/keroro\.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /keroro\.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?keroro.com$ [NC]
RewriteRule ^(/)?$ keroro.com/index.php [L]
RewriteCond %{HTTP_HOST} ^(www.)?doraemon.com$ [NC]
RewriteCond %{REQUEST_URI} !^/doraemon\.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /doraemon\.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?doraemon.com$ [NC]
RewriteRule ^(/)?$ doraemon.com/index.php [L]
Thus, if I go to www.hellokitty.com, it rewrites to ./htdocs/hellokitty.com/ internally.
Problem
If I go to www.hellokitty.com/hellokitty.com/
Or, if I go to www.hellokitty.com/keroro.com/, it still works
So, my problem is that how to prevent the client direct access the above real URLs and show a 404 error to them?
Insert this rule just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /(hellokitty|keroro)\.com [NC]
RewriteRule ^ - [F]

selectively grab portion of query string from url via url rewriting in htaccess

I have this in my .htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=(?!admin)(?!superadmin)((?![^&]*?edit)[^\s&]+) [NC]
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
This rewrites in both directions. One way from:
domain.com/index.php?p=Home&l=en&m=0
to:
domain.com/Home
and vice versa. What I would like to do is preserve the query string part of the URL that comes after the value of 'p' and pass it along BOTH WAYS.
I was able to do one way, with this code:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=(?!admin)(?!superadmin)((?![^&]*?edit)[^\s&]+) [NC]
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+)&p=([^\s&]+) [NC]
RewriteRule ^ /%1?%2? [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
but I can't get the other way right. Can you guys help? I basically want the value of p to be written as part of the URL after the domain and I don't want it to reappear again but I want all the other variables to be appended.
Right now what I get is:
domain.com/Home?m=0?&p=Home&ms=m0&l=en
This should work for you:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=((?!(?:admin|superadmin|[^&]*?edit))[^\s&]+)(?:&(\S+))? [NC]
RewriteRule ^ /%1?%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]

.htaccess multi RewriteRule and RewriteCond help me to combine them together

I need help with the following .htaccess, i need to combine them together if its possible
This will redirect to the domainname.com.au with http://www at the front of domain name if there is no http:// or www at the front:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domainname.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domainname.com.au$
RewriteRule ^(.*)$ "http\:\/\/www\.domainname.com.au\/$1" [R=301, OR]
And this one should get the variables that passing at the end of the domain name after slash /, I should see "/contact" but the php MVC see this "/index.php?url=contact"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
deny from 111.93.163.217
Thanks!
The two rules can't be combined as they're quite different, but they can be shortened:
# Force www
RewriteCOnd %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Rewrite parameter
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [B,NE,QSA,L]