htaccess redirect to remove duplicate pages - php

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. :)

Related

htaccess rewrite rule for affiliate id

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.

Mobile Redirect using htaccess

My site is:
example.com
My mobile version:
m.example.com
Profile page is
example.com/profile or m.example.com/profile
Or posting page
example.com/posts or m.example.com/posts
Mobile version's url is similar. So, how to redirect the same page?
For example: user go (from facebook) into example.com/posts but he uses mobile device,
so how to redirect via .htaccess to m.example.com/posts
I saw this one threat, but i'm confuse to create right rules.
Thanks :)
Your question is similar to this question, except that you want to redirect the user. First you need a list of mobile user-agents. I am not going to suggest a list, but I think you should be able to find a list that is suitable for you.
Next, you want to go to your .htaccess file and add something like this:
RewriteCond %{HTTP_USER_AGENT} ^(user-agent1|user-agent2|user-agent3|etc)$
RewriteCond %{HTTP_HOST} !^m\.example\.com$
RewriteRule ^(.*)$ http://m.example.com/$1 [R,L]
The first RewriteCond contains all user-agents you want to redirect. Remember that it is a regex, so you have to escape any special characters! The second RewriteCond checks if the host isn't already m.example.com. If we wouldn't check for this, we would create an infinite loop, redirecting to the same url over and over. The RewriteRule itself matches everything after the hostname (and before the query string) and redirects it to the mobile site. $1 gets replaced with the first capture group in the regex in the first argument of RewriteRule. The [R] flag is not necessary in this case (links with http:// will always be redirected), but it let's Apache know that it should do an external temporary redirect to the new url. The [L] flag will stop processing more rules if this rule matches.
See the documentation for more information.

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]

Restrict access to old files after URL Rewrite

I have a website developed in PHP. I have recently done the URL rewriting which works fine. However, I just found out that my pages with parameters are also accessible. For eg.
I converted this URL
domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy
After rewrite it looks like this
domainname.com/products/5/Samsung-Galaxy.html
Everything works just fine. However, My site is still accessible using the old parameters. I want if someone types in the old URL should be automatically redirected to Ideally New Page if not then index page. Google and MSN shouldn't access these pages with parameter. Any help will be highly appreciated.
Thanks for your input. Here is more detail to my question.
I converted this URL
domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy
After rewrite it looks like this
domainname.com/products/5/Samsung-Galaxy.html
The code looks like this.
RewriteRule ^products/(.*)/(.*).html$ index.php?page=product&pid=$1&proTitle=$2 [nc]
Rewrite Works fine. However, if I try to access old URL i.e domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy the page is still accessible and on top of that being crawled by Google and other search engines. I want If someone tries to access this URL, it should direct them to Page Not Found and this should also not be sniffed by any crawlers.
Thanks a lot again for your time and I hope I can get your valuable reply soon.
You need another rule which redirects the browser to the nicer looking URL if the request is made for the ugly looking one. For example:
# need to replace spaces with "-"
RewriteRule ^(.*)\ (.*)$ /$1-$2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^&\ ]+)&pid=([^&\ ]+)&proTitle=([^&\ ]+)
RewriteRule ^ /%1/%2/%3 [L,R=301]

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