Dynamic htaccess rewrite - php

I am trying to figure out how to setup some redirects in my htaccess file.
We load our stores with dynamic urls for example:
/locations/new-york
new-york being the dynamic part
We have old urls that contain different path such as
/locations/new-york/local-store
I want to be able to take all locations/dynamic-city and forward them to the new URL
So
/locations/new-york/local-store/offers
should goto /locations/new-york/current-offers

RewriteRule ^locations/(.*)/local-store/offers locations/$1/current-offers [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/hot-tubs locations/$1/hot-tubs [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/pre-owned locations/$1/pre-owned [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/client-reviews locations/$1/reviews [L,R=301,QSA]
Ok, so I'm not seeing a way to easily use 1 rewriterule to cover all 4 cases. But you can see make sure the requested url following a pattern like this.
^locations/{location}/local-store/offers
Then you take the location and place it into the new url you want to write it to.
L # means it's the last rule, don't try to match anymore
R=301 # means redirect the old url to the new one
QSA # means append any query string parameters to the new url

Related

htaccess rewrite rule for url varible as parent folder

I already create a .htaccess in my folder and would like to make the URL:
www.example/good/page
toward:
www.example/page?type=good
my current document is written as:
RewriteRule ^(\w+)$\/page page.php?type=$1 [NC,L]
but it didn't work, and I don't know how to check it is correct or not
would anyone able to provide some example for that?
thanks!
Make sure your rewite engine is on, and it is rewriting the base. Change your code like the following:
RewriteEngine on
RewriteBase /
RewriteRule ^/?([^/]+)/page?$ "page.php?type=$1" [L,QSA]
And then your rewrite rule must look like this.
The RewriteRule basically means that if the request is done that matches ^/?([^/]+)/page?$ (matches any URL except the server root), it will be rewritten as page.php?type=$1 which means a request for page.php be rewritten as page.php?type=good).
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite (example.com/good/page?id=2 will be rewritten as page.php?type=good&id=2.
L means if the rule matches, don't process any more RewriteRules below this one.
Try this and let me know. Accept the answer if it worked for you.

.htaccess - Rewrite with 3 variables but after that get the rest of the URL

So I'm currently using my .htaccess to write my URL to be more URL friendly.
But I'm having trouble getting the fourth variable that could be more folders. I'll try and explain it as best I can below.
So at the moment I have the following URL working:
www.mysite.co.uk/first/second/third
Now for this I am using the rewrite rule:
RewriteRule ^e/([a-zA-Z0-9-/]*)/([a-zA-Z0-9-/]*)/([a-zA-Z0-9\-\/]*)$ index.php?var_1=$1&var_2=$2&var_3=$3 [L]
My problem comes when I one of the following happens:
www.mysite.co.uk/first/second/third/fourth
www.mysite.co.uk/first/second/third/fourth/fifth
www.mysite.co.uk/first/second/third/fourth/fifth/sixth
From the above I want the following variables to be returned:
$_GET['var_1'] = first
$_GET['var_2'] = second
$_GET['var_3'] = third/fourth
OR
$_GET['var_3'] = third/fourth/fifth
OR
$_GET['var_3'] = third/fourth/fifth/sixth
So basically I always have a 'var_1' and 'var_2' set but the third variable can be the rest of the URL.
I hope this makes sense and that it's possible.
I'm not sure why you your rule starts with ^e/
if your urls will be in format : www.mysite.co.uk/e/first/second/third/fourth
then use this regex:
RewriteRule ^e/([a-zA-Z0-9-]+)(/([a-zA-Z0-9-]+))?(/([a-zA-Z0-9-/]*))?$ index.php?var_1=$1&var_2=$3&var_3=$5 [L]
If you need it without /e/ at start www.mysite.co.uk/e/first/second/third/fourth
then remove /e/ from start
RewriteRule ^([a-zA-Z0-9-]+)(/([a-zA-Z0-9-]+))?(/([a-zA-Z0-9-/]*))?$ index.php?var_1=$1&var_2=$3&var_3=$5 [L]
Example above will work even with url-s like /first and /first/second but if you don't need it and you want to have this rule just in case of 3 or more params then you can simplify rule:
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-/]+)$ index.php?var_1=$1&var_2=$2&var_3=$3 [L]

htaccess friend url in php

I'm trying to create some 'friendly URL' to my site. But I'm not sure what to do. Below as my site is structured:
To enter a page for example: localhost/study/dashboard/client/network.php
I wanted to write to localhost/study/network. Following the same format to some other pages. I wanted to block direct access to critical files, such as user-proc.php, api.php for example.
What I tried to do:
RewriteEngine On
RewriteRule ^study/network?$ network.php
I put a .htaccess file in each directory (administration, client), but that blocked my website for full. I could not even more access for localhost/study/administration/network.php.
To rewrite from /study/network to /study/dashboard/client/network.php, you can just put the two URLs, pattern and substitution, in a RewriteRule
RewriteRule ^study/network$ /study/dashboard/client/network.php [L]
This is for this specific case. To do this for any combination, you must capture the two parts from the request URI pattern and then use this in the substitution part
RewriteRule ^(.*?)/(.*?)$ /$1/dashboard/client/$2.php [L]
This looks for any two component part (...)/(...) and uses this - $1 and $2 - in the replacement.
To prevent access to some URI, you do almost the same, but omit the substitution part with a - (dash), and use the flag F|forbidden
RewriteRule user-proc.php - [F]

htaccess - Redirect to the same location irrespective of presence of trailing slash

I want to redirect the following sets of links:
a/b/c or a/b/c/ to a.php?b=c
x/y1/z1/y2/z2 or x/y1/z1/y2/z2/ to x.php?y1=z1&y2=z2
using htaccess and mod rewrite in a standardized general format associating the appropriate PHP get tags and values to the corresponding SEO-friendly link. How do I do so?
I've tried tinkering around with RewriteCond and REQUEST_FILENAME but just cannot seem to get it to work.
Something like this might help:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? /$1.php?$2=$3 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/? /$1.php?$2=$3&$4=$5

Deleting all php extension and get rid of variables in the url

I got this url
/localhost/andalucia/productdetails.php?value=20
I want to change it to
/localhost/andalucia/productdetails/20
how do you do this and how will you get the value 20 in the handler page?
should I change the coding or I just add an ht access file?
If it is adding a ht access file what code should be in it?
How if I have more pages like:
/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus
Will they be affected automatically too?
ok i tried to use
RewriteRule ^productdetails/([0-9]+)$ productdetails.php?value=$1 [L,QSA]
but now the problem is all my pictures is gone in the html and i cant open the other page like
/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus
i need a htaccess code that can open all of these
/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus
/localhost/andalucia/productdetails/20
pls helpp someone
With the apache extension mod_rewrite it is really easy to transform your pretty URLs into the URLs needed by your script. This .htaccess example which you place in your web root should get you going:
RewriteEngine On
RewriteRule ^andalucia/productdetails/([0-9]+)$ /andalucia/productdetails.php?value=$1 [L]
This example will only rewrite andalucia/productdetails/NNNN... format URLs, all other URLs won't be affected. If you need to pass other query parameters, like /andalucia/productdetails/20?sort=asc you need to pass the QSA flag (query string append) to the rewrite rule:
RewriteRule ^andalucia/productdetails/([0-9]+)$ /andalucia/productdetails.php?value=$1 [QSA,L]
The L flag will prohibit the evaluation of next rules. Just look up the mod_rewrite documentation for a in-depth discussion!

Categories