htaccess rewrite url using last parameter - php

I have a query on htaccess i need to rewrite a url like below example
www.example.com/parameater1/parameater2/parameater3/name=xyz
i need something like below
www.xyz.example.com/parameater1/parameater2/parameater3
i tried the below code
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.co\.uk$
RewriteRule ^(.*)$ "http://www.example.co.uk/test/$1/$2/$3?user=%1" [P]
but unable to resolve using the above code

Not best, but should work.
RewriteRule ^([^/]*/[^/]*/[^/]*)/name=(.*)$ http://www.$2.example.com/$1 [R=301,L]

Related

.htaccess rewrite to parent when parameter is given

I have a problem with a rewriterule in my .htaccess, I would like to redirect like this:
www.mywebsite.com/category?p=2
to
www.mywebsite.com/category
where category is variable.
I have my rewrite engine on and other redirects are working fine. This is what what I got so far:
RewriteRule ^/([a-z_\/]*)\?? /$1 [R=301,L]
What is the best way to accomplish this?
Your request looks strange, but anyway, this should do it:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=
RewriteRule /?([a-zA-Z0-9]+)/? /$1? [R=301,L]
Use this:
RewriteEngine On
RewriteRule ^category$ /category?p=2 [L]
It will give you the following URL:
www.mywebsite.com/category

url rewrite and redirect - php

I recently developed a web site using php which accepts a id as its input via get.
http://website.com/profile.php?id=123. its the old fashion way, I've seen sites use pretty url's like http://website.com/username.html
So in order to archive this, i managed to do some research and end up with this code blow.
Also i'm passing the username in the query string.
http://website.com/profile.php?id=123&username=test_user
the parameter ID is only used as a input for the profile.php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /profile.php [NC]
RewriteCond %{QUERY_STRING} ^id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%2.html [R=301,L]
The final output i seek is http://website.com/username.html
Is their anything wrong in this ? this doesn't seem to work. Am i doing something wrong ? if so please be kind to let me know where my error lies. Thank you.
Try this rule:
RewriteEngine On
RewriteRule ^profile/([^/\.]+)/([^/\.]+).html/?$ profile.php?id=$1&username=$2 [L]
then you should be able to use
http://website.com/profile/123/username.html
as the URL. You can ignore the profile part but I don't recomment it because it would clash with other URLs of your website.
If you only need the username, use
RewriteRule ^profile/([^/\.]+).html/?$ profile.php?username=$2 [L]
and the URL would be
http://website.com/profile/username.html
You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /profile.php\?id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^ /%1/%2.html? [R=301,L]
RewriteRule ^([0-9]+)/([^/.]+)\.html$ profile.php?id=$1&username=$2 [L,QSA,NC]

.htaccess if request url diffrent from some string than

i have a trivial question about htaccess rewrite condition.
The think i want to do in my htaccess is:
When someone accesses my project from a url different from "example.com" to be redirected to a specific controller, for example app_user.php
I need the syntacs for this condition redirect.
Thanks for your time
Try this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/app_user.php
RewriteRule ^ /app_user.php [L]

htaccess condition in 301

I am sure there are other threads on similar topics around the web. But I just can't get this working on my own website. Please help me
My site working in my subdomain
for eg http://demo.example.com/sitename/india
if i am put 1 parameter like (india) only. its loading profile.php?countryname=india also its working good.
But when i add multiple parameters into my url like this
http://demo.example.com/sitename/india/tamilnadu/chennai
i want load profile.php?countryname=india&state=tamilnadu&city=chennai .
I am try this code
RewriteCond %{HTTP_HOST} ^demo\.example\.com/sitename$
RewriteRule (.*) walola/$1/$2 [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ sitename/profile.php?countryname=$1&state=$2 [QSA]
When i pass single parameter into my url its working otherwise its shows 404 error.
Please guide me.
***Answer***
RewriteCond %{HTTP_HOST} ^demo\.example\.com/sitename$
RewriteRule (.*) sitename/([^/.]+)/([^/.]+) [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ sitename/profile.php?countryname=$1&state=$2 [QSA]
Its Works good.
Please try this
RewriteRule ^(.*)/(.*)$ sitename/profile.php?countryname=$1&state=$2 [QSA]
or else send the full url to the profile.php page and split by using php.
i.e.,
RewriteRule ^/* sitename/profile.php?site=%{REQUEST_URI} [L,NC,QSA]
I would do it like this:
1. RewriteRule ^sitename/(.*) /sitename/profile.php/$1 [QSA,L]
2. In PHP: $args = explode("/", $_SERVER["PATH_INFO"]);
3. By var_dump($args) you will have idea how to deal with it.

Dynamic subdomain with .htaccess and php

I have a page like
index.php?username=thurein
i need to get with subdomain like
thurein.mydomain.com
how could i get it?
I have done like ..
RewriteEngine on
RewriteCond %{HTTP_HOST} !^mydomain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).mydomain.com [NC]
RewriteRule ^$ /index.php?username=%2 [QSA]
But I couldn't pass other variables.
something like ...
thurein.mydomain.com/photos/1
what I want is that url re write to ..
index.php?username=thurein&page=photos&id=1
Thanks
If you can help me .. I do really appropriate for it.
i do this on my htaccess and it works
RewriteEngine on
RewriteCond %{HTTP_HOST} !^mydomain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).mydomain.com [NC]
RewriteRule ^([a-z0-9-]+)/$ /subdomain.php?subdomain=%2&menu=$1 [QSA]
for index.php?username=thurein&page=photos&id=1
RewriteRule ^([a-z0-9-]+)/([0-9]+)/$ /subdomain.php?subdomain=%2&page=$1&id=1 [QSA]
hope this solve your problem.
you can redirect all subdomains to main domain for example on index.php, in the script you can parse the url and assign $_GET['username'] = $parsedSubDomain; and the script will do everything like index.php?username=thurein.
I hope it is clear.
P.S you can do the redirection using cPanel if your hosting supports it, or you need to change httpd config.

Categories