How to set up htaccess rewrite for multiple variables? - php

I originally created SEO friendly links for my site. I wanted my links for my coupon code site to look like:
http://www.mydomain.com/site/bodybuilding.com
So I did a rewrite in the htaccess that worked:
RewriteRule ^site/(.*)$ retailertest3.php?linklabel=$1
Now, I'm working on creating a custom affiliate program. I want users to be able to send traffic to a link like:
http://www.mydomain.com/site/bodybuilding.com?ref=john
How would I go about modifying the rewriterule so that the ref variable is passed properly to retailertest3.php?
Also, after I get the value of the ref variable and do some stuff with it, is it possible to do a 301 redirect back to the original URL of:
http://www.mydomain.com/site/bodybuilding.com
I'm trying to avoid possible canonical issues like this, as well as keep everything looking clean.
Any help is greatly appreciated!!!!!
Any help is greatly appreciated!!!!

This should do the job:
RewriteRule ^site/(.*)$ retailertest3.php?linklabel=$1&%{QUERY_STRING}

Related

How do I change the url look with .htaccess but keep the php functionality?

I'm trying to learn, so please be kind. My site is database driven and uses ID numbers to generate the pages. I'd like the URL not to show the ID number but the name of the page. Is there a way to do this where it is just cosmetic and doesn't effect the site? I'm also most likely screwing up the way I'm writing the redirect as it doesn't seem to work at all? What am I doing wrong? Thank you.
This is how my URL looks now:
http://mydomainname.com/index.php?id=35-Entertainment
I'd like it to look like this:
http://mydomainname.com/Entertainment
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [L,QSA]
Unfortunate there is not a way to do this.
The process in this case is the following:
A user enters the a url. In this case you want a nicely formatted URL (http://mydomainname.com/Entertainment)
The user sends this url to your server
Apache processes the url and points it to your project directory based on the domain used in the url
There Apache uses the htaccess to see what to do next. In this case the best method would most likely be to use RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] or something like that
Then you need to do the routing within your app. You can use a routing table for instance to map the nicely formatted slug to an module/id combination
This process can't be done by Apache for it does not known about the content present in your database.
Hopefully this is of any help for you.

htaccess change requested filename

I need to do this using htaccess
When a request is made for http://www.example.com/home, it should (internally)load the page http://www.example.com/home_st.php
Similarly when a request is made to other link called products (http://www.example.com/products), it should (internally)load http://www.example.com/products_st.php
In short what it is doing is appending "_st.php" and loading that URL. But one thing I do not want is if the user directly types http://www.example.com/home_st.php or http://www.example.com/products_st.php in the browser, it should show 404 / Page not found error
I have few other pages in that folder and I want those pages to behave in this manner. I understand the htaccess should have something like this
Turn on the rewrite
Forbid access if the URL is called with page names like home_st.php, products_st.php etc.
If it's "home" or "products", then rewrite(append?) it to home_st.php and products_st.php respectively. I have other files too while need to follow the same
P.N: My URL should not show the actual filename, for example home_st.php, products_st.php etc. It should only show as http://www.example.com/home, http://www.example.com/products etc
htaccess and regex is not something that I am well acquainted with. Any help would be great. Thanks
You want to be able to re-write URL's
This has been written before but i'll say it again.
You want to use the Htaccess file and the re-write rule.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^pet-care/?$ pet_care_info_01_02_2008.php [NC,L] # Handle requests for "pet-care"
This will make this url: http://www.pets.com/pet_care_info_07_07_2008.php
Look like this: http://www.pets.com/pet-care/
Links to more information: How to make Clean URLs
and for the webpage I used to reference this information from: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
To get your pages set the way you want them, I would advise that you read the two articals and try get what you are looking for.
If you need some specific help with doing this, ask.
Hope that helps.

Rewrite to add a query string in wordpress

I know this can be done using a wordpress function but I'd like to add this rule in my htaccess file.
Basically I want to add a parameter to the URL and then let wordpress go about its business..
I have this
RewriteRule ^events/all/ /events/?all [L]
But I'm getting a 404 when I try and go to /events/all/
Thank you for any advice
Joe
Make sure you add that rule before any wordpress rules. Wordpress routes everything to index.php and if the routing happens before any specific rewrites that you have, those rewrites will never get applied.
Additionally, wordpress uses the request information to handle routing, so it may see /events/all/ as the $_SERVER['REQUEST_URI'] var and get confused. So wordpress may not be able to go about its business, depends on how you've setup the wordpress end of things.
If for whatever reason it's still not playing nice with wordpress, you could try adding the P option to internally proxy the request:
RewriteRule ^events/all/ /events/?all [L,P]

single .htaccess for multiple pages redirect with differnt url

i am changing my website and its old pages were like bellow
http://mydomain.com/keyword_city.html
while i read that its better to use - in page for batter SEO, so i am thinking to use new new page name like bellow
http://mydomain.com/keyword-city.html
first of all i want to know is this better idea to change page URL?
and if i change my URL does this effect on page ranking ?
i want to know how to write single .htaccess so that any request coming for old page will redirect to new URL, the only difference in old and new URL will be _ to -
Thanks
I think you are probably looking for:
Redirect 301 /keyword_city.html /keyword-city.html
to be placed in your .htaccess file.
As other people have mentioned though, probably not going to help a great deal when it comes to SEO. One thing that could however is using rich snippets/structured data:
https://support.google.com/webmasters/answer/99170?hl=en
Can be a lot of effort to do but would really help your presence on Google especially.
The following code added to .htacess should redirect all underscores _ to hyphens -
RewriteRule (.*)_(.*) $1-$2 [N]

.htaccess & wordpress site - How to add 301 redirect rules for no longer valid permalink?

I recently did a big update on my wordpress site, thus some old permalink are invlid now.
Seach google for a tutorial on this and found a lot about static html page redirection examples or specific php page which are not what i'm looking for
Both my old and new permalinks are in path format, for example, i need redirect a couple urls of old gallery posts (deleted) which are
/2009/06/gallery/abc/
/2009/06/gallery/cba/
/2009/06/gallery/bbc/
/2009/06/gallery/aab/
to a new page which is
/gallery/
How do i write a correct redirect rule for this?
RewriteRule ^[0-9]{4}/[0-9]{2}/gallery/.*$ gallery [R=permanent,L]
Should get you to where you need to be. If you need the actual parameters (IE Date / Month / Title) passed along it is a bit of a change up, but do-able.
EDIT
Fixed typo.
Try something like this:
RewriteEngine On
RewriteRule ^/2009/06/gallery/\(?([^/]*)\/$ /gallery/ [R=301,L]
Personally, I would use the Redirection plugin rather than editing your .htaccess directly -- it's easier, less danger-prone, and will let you log what redirections have been happening. It will also track 404 errors so you can see if you've forgotten to redirect anything.
Then, if you're just redirecting a couple of fixed posts, it's just a matter of adding a simple rule for each redirect, with the old and the new URLs, on the Redirection configuration page. You can also use regular expressions, as with .htaccess, if you want to do anything more complicated.

Categories