How to rewrite "id" url-parameter on a Joomla installation? - php

We have accidentally sent out a number of emails to customers with incorrectly formatted links. In particular, a URL parameter was named "id" where it should have been "ri".
I'm having a hard time setting up a rewrite rule that handles these links and takes customers to the correct place anyway. It seems that Joomla is always "snatching" up the "id" parameter before my Rewrite rules apply and misinterprets it, thus leading to unnecessary 404 errors.
This should in theory work:
RewriteCond %{QUERY_STRING} id=([slg][^&]+)
RewriteRule ^([den]{2}/)?product/(.*)$ $1product/$2?ri=%1 [L,R=301]
In fact, when I replace the "id=" with something like "xyz=" then it does indeed work. However, no such luck with "id".
I have SEF-links and rewrites enabled in Joomla. The above rule is in the .htaccess file in the root folder (which also contains the Joomla default rewriting rules). Joomla-version is 2.5.14 . What do I have to do to make this work?

THere is no way for joomla to take action before the .htaccess
If you have that condition at as the first one, I would remove the L Parameter, because it can keep Joomla from building its own rewrite rule.
If you want to check, if the condition is met, simply write a rewriteRule http://www.google.com [L], to check if, the ID= Parameter is really identified

Few suggestions
I would advice adding QSA flag. This will keep both id and riparameters in resulting URL and then let different codes take whatever parameter they want
Make this rule as first rule
Test in a different browser to avoid 301 caching issues
Suggested rule:
RewriteCond %{QUERY_STRING} (?:^|&)id=([slg][^&]+) [NC]
RewriteRule ^([den]{2}/)?product/(.*)$ $1product/$2?ri=%1 [NC,QSA,NE,L,R=301]

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.

Redirect specific Url variables

My site used to generate URLs like this:
/data.php?s=1432862823&type=basic
I have modified the program so the new URLs generated are:
/d.php?s=1432862823&t=basic
Since some people have bookmarked the old URLs I want to write a Rewrite rule that will get them to the new url.
I've not this so far:
RewriteEngine on
RewriteRule "^/data\.php$" "/d.php"
but I can't figure out how to account for the variables.
Try this :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+)&type=([^&]+) [NC]
RewriteRule ^data.php$ /d.php?s=%1&t=%2 [NC,R,L]
This will externally redirect a request for :
/data.php?s=foo&type=bar
to
/d.php?s=foo&t=bar
Edit: I do apologise, I only noticed now that you have changed the type parameter to t. As such, this solution will not fit your needs, unless for a URI where the query string parameters do not change. I'm leaving this answer here so that others may learn from it - you'll be surprised how many people don't know that the query string is automatically transferred to the new destination. Starkeen's answer, therefore, is the correct answer.
You could follow Starkeen's solution, which specifically checks for the query string and explicitly adds it to d.php. Alternatively, for simplicity, you could just use this:
RewriteEngine on
RewriteRule ^data.php$ /d.php [R=302,L]
The query string will automatically be transferred from data.php to d.php, and you will be redirected accordingly.
To make the redirect permanent and cached by browsers and search engines, change 302 to 301.

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!

Is it possible to use mod_rewrite htaccess to rewrite a url from name to id.

If I have a URLhttp://www.domain.com/listing.php?company_id=1 is it possible for me to re-write that to http://www.domain.com/company-name by using that id to pull the name from the database.
Or do I have to change listing.php to make it ?company_name=company-name
If anyone can point me in the right direction that would be great.
Thanks!
A map function allows using no id in the url at all, while still using the id in the rewritten url to do the database lookup.
http://www.domain.com/another-one
maps to
http://www.domain.com/listing.php?company_id=3423
Here is how I use map text files, which I generate from a database query. I believe mod_rewrite also does mapping to a database directly, of which I'm unfamiliar (maybe someone can provide that answer). I use Helicon Tech's isapi_rewrite v3, which works like mod_rewrite, so this should work for you.
Sample map file named map_company.txt
some-company 12
another-one 3423
freds-fill-dirt-and-croissants 44
The rewrite rules:
RewriteMap map_company txt:map_company.txt [NC]
RewriteCond ${map_company:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^/(.*) /listing.php?company_id=${map_company:$1} [NC,QSA,L]
RewriteMap assigns the map_company.txt text file to a variable named map_company (named the same just to be consistent).
RewriteRule is doing the work. It captures everything after the slash into $1, then rewrites it to your listing.php url. The ${map_company:$1} is looking up the url in the map file, and returning the id.
RewriteCond is just doing a double-check to see if the listing is there. The $1 is coming from the RewriteRule url, the NOT_FOUND is a default value if not found, and the condition is if it's not-NOT_FOUND (a little tangled - but it's just checking if it's in the file). If it's in the file, the RewriteRule will be run. If it's not in the file, it skips the RewriteRule, and falls through to more rules (perhaps to a page-not-found or some other default).
You'll need the id in both the urls, but only use the full name in the pretty link for the user.
The following would rewrite http://www.domain.com/42/company_name to http://www.domain.com/listing.php?company_id=42 and just discard the company name.
RewriteEngine on
RewriteRule ^([0-9]*)/([A-Za-z0-9_]*)/$ /listing.php?company_id=$1 [L]
Note that a user could also visit http://www.domain.com/42/wrong_name and still land on the right page with the right company name. If this isn't desired you could change the rule to /listing.php?company_id=$1&company_name=$2 and check for equality in listings.php

Append a parameter to a querystring with mod_rewrite

I would like to use mod_rewrite to append a parameter to the end of a querystring. I understand that I can do this using the [QSA] flag.
However, I would like the parameter appended ONLY if it does not already exist in the querystring. So, if the querystring was:
http://www.mysite.com/script.php?colour=red&size=large
I would like the above URL to be re-directed to
http://www.mysite.com/script.php?colour=red&size=large&weight=heavy
Where weight=heavy is appended to the end of the querystring only if this specific parameter was not there in the first place! If the specific parameter is already in the URL then no redirect is required.
Can anybody please suggest code to put in my .htacess file that can do this?
EDIT: tested and modified it, now it works for me
I assume you want to add a default weight parameter if there is no weight at all:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} !weight=
RewriteRule ^script.php script.php?weight=heavy [R,L,QSA]
This is esentially the same what #coreyward answered, but a bit more specific. (R flag, to make the change visible, and the weight parameter does not have to be heavy.)
Hope this helps!
This is a really strange thing to do. It's as though you're setting a default for your system, which is a pretty standard thing to do. The problem is that it's unmaintainable and awkward to be setting that default in your VHost definition or .htaccess file where you're literally configuring Apache.
I advise against doing it this way, but this is a quick and dirty way to do what you're looking for:
RewriteCond %{QUERY_STRING} !weight=heavy
RewriteRule /script.php /script.php?weight=heavy [QSA]

Categories