htaccess rewrite rule for affiliate id - php

My site is just index.php and every page is read from that. I'm not using WordPress.
What I'm seeking to do is the same thing that WordPress does with its page structure. In Wordpress you can set the rewrite rule so that a subsequent subdirectory can pass a value to index.php.
I have a client that is building an affiliate network and the base URL is for example:
mywebsite.com/?id=viktor
They would like to parse URL's instead so that it's much simpler for the customers.
i.e. mywebsite.com/u/viktor would 'rewrite' to mywebsite.com/?id=viktor.
What would be the .htaccess rule or associated PHP I'd have to put in index.php?
Obviously we won't have a 'u' folder in the site. :)
Thanks for your help.
This related question I believe has the code we're looking for however I'm not sure how it would apply in this situation

You can do it like this:
RewriteEngine On
RewriteRule ^u/(.+)$ /?id=$1 [NE,L]
If you need to also preserve any existing query string (like /u/viktor?param=value), use this:
RewriteEngine On
RewriteRule ^u/(.+)$ /?id=$1 [NE,QSA,L]
Update
Well it's bad practice using relative URLs (for this reason), but if you really want to avoid updating all your code, which could easily be done with a script or a command, you could do this:
RewriteEngine On
RewriteRule ^u/(.+/.+) /$1 [NE,L]
RewriteRule ^u/(.+)$ /?id=$1 [NE,QSA,L]
Which would rewrite everything that is referenced in a folder under /u/ to root. But it breaks browser caching as all your resources now have two URLs. Really you should just update your URLs to absolute.
If the above doesn't work for you and you only want to rewrite specific folders, do it like this:
RewriteEngine On
RewriteRule ^u/((?:assets|images|css)/.+) /$1 [NE,L]
RewriteRule ^u/(.+)$ /?id=$1 [NE,QSA,L]
Replacing your folder names as appropriate.

The problem you have here, Viktor, is that WordPress checks the original URL when it is processing a request. This is because it has its own mod_rewrite rules to rewrite everything to index.php.
So you can't do what you want with mod_rewrite, because WordPress won't look at your rewritten URL, and will process it as /u/viktor. You can do it with a 301 redirect, but you already said you don't want to do that.
In order to make this work properly, you need to work with WordPress to make those URLs work, perhaps creating a plug-in to do it. I wouldn't suggest adding anything to WordPress's index.php as it will only get overwritten when WordPress is updated.

Related

URL Rewritng is working but auto redirect is not for my website

I have used URL rewriting with .htaccess with the below code. Problem is that if we paste the URL then it's working but auto redirection is not working.
.HTACCESS CODE:
Options +FollowSymLinks
RewriteEngine on
RewriteRule Sports-Equipment-Catname-(.*)\.html$ Sports-Equipment.php?Catname=$1
My URLS:
http://example.com/Sports-Equipment.php?Catname=Discus
If I paste direct URL from htaccess is:
http://example.com/Sports-Equipment-Catname-Discus.html
why URLs is not auto-redirected to new pages.
Please help me to fix it.
why URLs is not auto-redirected to new pages.
There is no "automatic" process here. You have no directives that implement the redirect.
My URLs: /Sports-Equipment.php?Catname=Discus
That's the problem, you need to change the actual URLs in your HTML source. This should not be resolved with a "redirect". Otherwise, the user can still "see" the old URL before they click the link. If they copy a link they are copying the "old" URL. Every time the user clicks a link they are redirected - which is slow, not just for the user, but also for your server (uses more resources).
However, you should implement a redirect if you are changing an existing URL structure. But this is to preserve SEO only. For search engines that have indexed the old URLs and for third party backlinks to the old URLs.
For that you would need to add the following "redirect" before the existing rewrites. For example:
# Redirect "old" URL to "new" URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^Catname=([^&]*)
RewriteRule ^Sports-Equipment\.php$ /Sports-Equipment-Catname-%1.html [QSD,R=301,L]
The condition that checks against the REDIRECT_STATUS env var ensures we only redirect direct requests and not rewritten requests by the later rewrite - thus avoiding a redirect loop.
The QSD flag discards the query string from the initial request.

htaccess redirect to remove duplicate pages

I'm very new to this thing with htaccess rules.
I basically want two things.
Redirect exampledomain.com to www.exampledomain.com. This works, the web host that is used had a FAQ page where they suggested the code I use. What doesn't work is this:
Remove the way to access the "example" page from both "/index.php?p=example" and "/?p=example" to get better search engine ranks. If this means accessing that page from something like "/example", that's a bonus.
I basically want to remove the duplicate links that is caused by the /index.php-variant to get higher search engine ranks. Having prettier links isn't required, just a bonus, the most important thing is that I need to remove the duplicate links.
Here's the code anyway:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^exampledomain.com$
RewriteRule ^(.*)$ http://www.exampledomain.com$1 [R=301]
RewriteRule ^([^/]*)\.html$ /index.php?p=$1 [L]
RewriteRule ^([^/]*)\.html$ /?p=$1 [L]
Have I misunderstood the [L/R=301/*]-flags totally?
EDIT: Ok, to clarify. I want to remove duplicate urls to get higher search engine ranks. What my code above apparently did was to add yet another url to access the same page. Since I only want one way, is there any way to redirect instead of just pointing the "prettified" link to the original?
When I think about it, would a redirection of exampledomain.com/index.php to exampledomain.com do the trick?
Thanks guys so far, sorry for being bad at explaining. :)

How do you let all the sub pages to the main page?

How do you let all the sub pages to the main page?
sampel.com/2/sampel.html
so
sampel.com/sampel.html
so all sub pages move to the main page
wear .htaccess
How do you let all the sub pages to the main page?
Here is you do that via mod_rewrite rules:
RewriteEngine on
RewriteRule ^[^/]+/([^.]+\.html)$ /$1 [L,NC,R=302]
RewriteRule ^([^.]+)\.html$ /pages/head.php?cari=$1 [L,NC]
This will redirect every /dir/file.html to /file.html
Please read the documentation and search for examples.
I am not sure if you want to have an external redirect, or an internal rewrite. For the rule I show below, I am assuming you want the url in the browser to change, and therefore want an external redirect. First you need to make sure that mod_rewrite is enabled in your apache install. Then enable the rewriteengine in your .htaccess file and write the rules below it. You'll need to use "RewriteRule". The first argument is a regex that matches the request. The second argument is what it is rewritten to. The third arguments are a list of flags. You can find information about them in the documentation linked above. You want to make sure the first argument doesn't match the part that it is rewritten to, or you'll create an infinite loop, eventually causing an error somewhere. The following rule should do what you want it to do.
RewriteEngine on
RewriteRule ^2/(.*)$ $1 [R,L]

How to use mod_rewrite and Url redirection at same time

I have a url like this
post_article.php?url=how-to-use-mod-rewrite
I am using mod_rewrite to make it
post/how-to-use-mod-rewrite
Code I am using in .htaccess
RewriteEngine On
RewriteRule ^post/([^/]+)/?$ post_article.php?url=$1 [L]
Now both of this urls will give the same content, which is not good in SEO point of view.
How I can redirect all those old urls to new url.
ie post_article.php?url=any-value to post/anyvalue
Use canonical URLs to help search engines understand duplicate content, if SEO is your sole concern.
RewriteEngine On
Redirect ^post_article.php?url=.*$ post/$1
RewriteRule ^post/([^/]+)/?$ post_article.php?url=$1 [L]
Try it.
Just create a function to rewrite all your links in your articles, and search engines will never be able to find the old url scheme.

Very advanced .htaccess

Okay, I've looked around a bit, and I cannot seem to find the answer.
Now I'm not asking for you to give me just a block of code, and say "this is the fix", I would like for you to explain how you did it, what you came up with, etc. I would really like to know more about this, and the only way to learn is by example.
Now what I need to do is:
Currently my blog (word press go figure, cause they own the world), is on the root directory. So my posts look like: http://localhost/hello-world. Just an example.
However, I've just extended my site, with a custom built PHP script using Code Igniter. I need to put the Code Igniter script on the root directory, and move the blog to /main/ or /blog/. I don't want to get rid of my blog, but I don't want Google to have to re-index every single blog post there is, or have Google lead to bad post urls. More importantly I don't want to have to sit here for hours and hours, creating redirect urls to each and every single blog post (theres hundreds of them).
Now I need the redirect to be a 301 redirect.
Heres the problem I have come across.
I NEED to have this in the .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|css|img|js|main|favicon.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
This removes the ugly localhost/index.php/controller/controller-function/
But I need to make my .htaccess redirect everything on the / but NOT the preset urls I already have.
The Preset url arguements I have through Code Igniter are:
/details/
/register/
/city/
/search_process/
/login/
/logout-success/
/login-success/
/logout/
/login/
/manage/
/panel/
So in essence..
localhost/hello-world/ would have to 301 redirect to localhost/main/hello-world
and
localhost/(any-of-the-above)/ would have to NOT redirect to /main/ or /blog/
Also if you notice in the current .htaccess, I've allowed certain things like /admin/ /css/ /img/ /js/ (/main/ is the blog obviously) and favicon.ico (cause it looks awesome)
Please school me! (How often do you hear that? :P)
CREDIT GOES TO LAWRENCE CHERONE!
He originally gave me the answer, and between then and now I think I made a typo the first time, and lost the code by accident, and it didn't work the second time after copy / pasting (cause the first time i actually typed it out to learn it).
So here goes with the modified working solution. I give full credit to Lawrence Cherone (the guy who has the accepted answer for this question) because he helped me figure this out, and ultimately I couldn't have gotten this working solution without him.. Thanks again bud!
RewriteEngine on
RewriteCond $1 !(panel|manage)
RewriteRule ^(.*)$ http://localhost/choosefitness/main/$1 [R=301,L]
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|css|img|js|less|favicon.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
This simply states, that as long as the first argument http://localhost/choosefitness/first-argument is no /panel or /manage. It should 301 redirect to /main/ (Had to provide full URL for 301 redirect, not just main/$1)
Then it states that any request made on admin css img js lss or favicon.ico should be ignored. else should be made on index.php (this removes the index.php from the url in code igniter)
I do not know why it works i just know it does. However I havent tested it fully but I believe to be able to access /css without beign redirected, you have to add |css to the first RewriteCond. However the server is able to access the css files without needing to do so.
This is untested & pretty sure index.php/$1/$2 is wrong but... You could include a rule that matches your CI rules and then pass to the CI controller, and if not match is found then rewrite to your /blog/ url. Also dont forget to escape the . in the favicon\.ico part.
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|css|img|js|main|favicon\.ico)
RewriteRule ^(details|register|city|search_process|login|logout-success|login-success|logout|login|manage|panel)/(.*)$ index.php/$1/$2 [L]
RewriteRule ^(.*)$ /blog/$1 [L]
Edit: If your parameter is at the front then switch the rule to something like this:
RewriteRule ^(.*)/(details|register|city|search_process|login|logout-success|login-success|logout|login|manage|panel)$ index.php/$1/$2 [L]

Categories