Rewriting URLs through htaccess with multiple versions of the same page - php

So I did quite a bit of research, but since I'm very new to manipulating .htaccess, I couldn't really find a good answer.
This is my site structure:
www.kanadax.ca/index.php is the landing page. This is what Guest users (in Joomla speak) see when they land on the page, before they signup.
www.kanadax.ca/index.php/anasayfa-2 is the home page for group 1 users, aka non-paying registrants.
www.kanadax.ca/index.php/anasayfa-3 is the home page for group 2 users, aka paying registrants.
Now ideally, I would like to make all of these just be www.kanadax.ca/ in the URL bar. The server should know which one to send the user to, but the end user should just see www.kanadax.ca/. I'm not quite sure if this is possible, so this is my first question. If so, what should the .htaccess look like?
If this isn't possible, can I at least take out index.php from each? So they would be
www.kanadax.ca/
www.kanadax.ca/anasayfa-2
www.kanadax.ca/anasayfa-3
Now of course, anasayfa-2 and -3 are not good practice (anasayfa=home) so I would be very thankful if someone can walk me through removing those if it's not possible to simply make all of these be displayed as www.kanadax.ca/
I hope I was able to explain things clearly but please don't hesitate to ask questions.

This is usually solved by enabling Use URL Rewriting and Search Engine Friendly URLs within the Global Configuration settings for Joomla. Be sure you have a Joomla .htaccess file aswell. There should be a provided htaccess.txt file you can copy to .htaccess if you don't. Additionally, confirm your Joomla cache has been cleared and your browser cache has been cleared before attempting to check for changes.

you can redirect your index.php page to your domain
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ http://www.kanadax.ca/$1 [R=301,L]
althogh you cannot redirect user to a specific page because info are stored in joomla session and you cannot make any rule or condition in htmlaccess basing on that.

Related

How to keep the same URL for both versions - mobile and desktop?

Is it possible to have the same URL for both the mobile and desktop versions? I don't mean a responsive site, but a completely separate mobile site with his own index.php, css and js files.
I would like the URL to remain unchanged and if the user enters from a mobile device to the website example.com it redirects to the appropriate index of a mobile version but the URL still remains example.com not m.example.com.
How can that be achieved?
The answer is Yes.
For example in Apache you can use mod_rewrite module and redirect the request to different index.php file depending on the client user agent.
Example:
RewriteCond %{HTTP_USER_AGENT} "..." [NC]
RewriteRule .* mobile.php [L]
RewriteRule .* desktop.php [L]
Also you can use one index.php as entry point, but you application can load different templates (theme) depending on the client user agent.
Personally i would recommend to search in apache context to archive your goal. Basicly you just want to redirect the user to different vhosts and you want a device detection to select the right vhost. Please correct me if im wrong.
I will link a short acticle here where you can find a detailes tutorial to get this done.
Now you just need a good rewrite rule in your vhost-configuration to keep the domain unchanged. Deep tutorial about rewriterules
If you already use a front controller for both your sites.
You can replace the existing one with something like this:
<?php
$front_path = is_mobile() ? '/path/to/mobile/front.php' : '/path/to/default/front.php';
chdir(dirname($front_path));
require_once $front_path;
function is_mobile() {
// Your logic here. Probably sniffing headers.
}
This with a name-spaced folder structure:
$static_base_url = '/static/mobile';
Then a CSS url would look something like this:
$static_base_url . '/css/main.css'
If you already have your application adapted for use with a CDN then you may not need these changes.
Your assets may already be indexed by search engines, and restructuring may not be possible. If so perhaps look at conditional reverse proxying.
(Rafts of existing rewrite rules and redirects may also complicate matters.)

writing htaccess rules for redirection

I have a website, lets say www.domain.com
and my current blog is at www.domain.com/blog/
so my URLs are like www.domain.com/blog/recent-post/
now Im having a new wordpress installation to handle blog, and pointed a subdomian to it, so the blog now is blog.domain.com
I have lot of seo work dne forwww.domain.com/blog/ I dont want to miss that, and I want to redirect users visiting www.domain.com/blog/ to www.blog.domain.com/
and www.domain.com/blog/any-url/ to www.blog.domain.com/any-url/
can some one help in writing redirection rule for this?
If in spite of #Cailbuig's advice you still want to do this, what you'd need is
Options +FollowSymLinks -MultiViews
RewriteRule ^blog(/.*)?^ http://blog.domain.com$1 [L,R=301]
Start with R=302 and then when it works change to R=301. 301 reponses are cached by browsers, so they are hard to test.
Why do you want to change domain.com/blog to blog.domain.com?
This isn't good for SEO, since the links do not have the same value for a directory that for a subdomain.
Better to have the blog in a folder that subdomain, since the authority will always go to main domain (domain.com).

.htaccess url rewrites for white label sites

I'm building a simple site that will only have a homepage and a contact page and wondered if I could use .htaccess to rewrite the urls for different companies.
So for example if I go to website.com/companyname-contact/ it will display that url in the browser bar but actually load a generic contact.php page, I can then use php to pull in the correct contact details for that specific companyname.
This would need to work for different company names (e.g. website.com/anothercompany-contact/) but only work for an array of approved company names.
I realise this may not be possible but I thought I'd ask because i spent about 4 hours this morning Googleing it with no real progress.
Thanks
Unless you want to manually list the approved company names in your .htaccess file (which looks UGLY) I'd suggest this:
RewriteEngine On
RewriteRule (.*)-contact$ /contact.php?company_name=$1 [L,QSA,NC]
and then in your contact.php
determine if valid company name - check db or whatever method you are using. (Make sure to escape the input)
if not valid you have a couple options:
redir to your default 404 page
issue an intelligent warning page (ie include suggestions for alternate spelling that is in the db) and set a 404 header. (better IMO)
if similar company name in the db possibly redirect to that with a note at the top of the page
Yes you can. You need to enable the rewrite engine, and then you will be able to use regular expressions to accomplish what you're trying to do.
This is an example of what your htaccess could like:
RewriteEngine On
RewriteRule ^contact/([A-Za-z0-9-]+)/?$ contact.php?company=$1 [NC,L]

Keep old website (HTML files) on webserver but disallow search agents to index them

I’ve just finished a website for a client who is going to replace their old (very old, HTML hard-coded website). The problem is that they (for now) want to save their old website and all the files on the webserver in the original position. This does not create any issues with the new website which is made in PHP and Wordpress but it makes a big deal when Google (and others) are dropping by with their search robots and indexing.
When doing a Google search it still finds the old HTML files. Is there any way that I could “keep” the old HTML files on the web server but make sure that for the first no robots are going to index them and if anyone is trying to navigate to an HTML page, e.g. http://www.clientdomain.com/old_index_file.html, they are getting redirect? I think the last part might be able to be done in .htaccess but I haven’t found anything useful searching for it.
The first question about not allowing robots and agents to index HTML files, I’ve tried to put these two lines in my robots.txt file
Disallow: /*.html$
Disallow: /*.htm$
But I’m unsure if it will work?
I might deal with this in a completely wrong way but I’ve never tried that a client has requested to keep the old website on same server and in original location before.
Thanks,
- Mestika
<?php
$redirectlink = ‘http://www.puttheredirectedwebpageurlhere.com‘;
//do not edit below here
header (‘HTTP/1.1 301 Moved Permanently’);
header(‘Location: ‘.$redirectlink);
exit;
?>
This code will use a 301 redirect the page to the URL that you desire. The filename of this .php should be the URL slug of the page you want to redirect.
301 Redirect
A 301 redirect, or also known as a permanent redirect, should be put in place to permanently redirect a page. The word ‘permanent’ is there to imply that ALL qualities of the redirected page will be passed on to the detour page.
That includes:
PageRank
MozRank
Page Authority
Traffic Value
A 301 redirect is implemented if the change you want to make is, well… permanent. The detour page now embodies the redirected page as if it was the former. A complete takeover.
The old page will be removed from Google’s index and the new one will replace it.
Or you can do it in your htaccess like shown by the above poster.
There's probably a lot of ways to handle this, assuming you have a clear mapping of pages from the old template to the new one, you could detect the Google bot in your old template (see [1]) and do a 301 redirect (see [2] for example) to the new template.
List item
[1] how to detect search engine bots with php?
List item
[2] How to implement 303 redirect?
Will take some work, but sounds like you'll need to crack open your htaccess file and start adding 301 redirects from the old content to the new.
RewriteCond %{REQUEST_URI} ^/oldpage.html
RewriteRule . http://www.domainname.com/pathto/newcontentinwp/ [R=301,L]
Rinse and repeat
This is definitely something mod_rewrite can help with. Converting your posted robots.txt to a simple rewrite:
RewriteEngine on
RewriteRule /.*\.html /index\.php [R]
The [R] flag signifies an explicit redirect. I would recommend seeing http://httpd.apache.org/docs/2.4/rewrite/remapping.html for more information. You can also forbid direct access with the [F] flag.

How to make extension-less url for a PHP based site?

Do I have to put every file in a different folder?
like:
about-us/about-us.php
profile/profile.php
etc.
or is there any other automatic solution.
I want to convert
http://sitename.com/about-us/about-us.php
to
http://sitename.com/about-us
You want pretty URL rewriting.
An Apache .htaccess examples from that article:
Pretty URL: /browse/animals-24/cats-76.html
Ugly URL: /browse.php?category=24&subcategory=76
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^browse/[A-Z0-9_-]+-([0-9]+)/[A-Z0-9_-]+-([0-9]+)\.html$ browse.php?category
If you have a directory called about-us, then you could simply create an index.php file within that directory, and by default, your .htaccess file should redirect users to the correct page.
Thus, going to example.com/about-us/ would bring the user to the same page as about-us.php. It would benefit you to do some research about 301 redirection.
http://www.tamingthebeast.net/articles3/spiders-301-redirect.htm
Basically, when Googlebot crawls your website, the last thing you want is for Google to find both copies of the page, one listed as about-us/ and one listed as about-us/about-us.php. Duplicate content is bad, and optimizing your website for search engines is really not at all that difficult to do.
Let's say you have a leaderboard page on your website with 1000 members. Instead of Google finding all leaderboard.php?user=Whatever, it would be a good idea to block that page from being accessed by Google, or else you will result in hundreds of unwanted archived pages on their search engine.
You might also want to make sure your website can be accessed either by www.yourwebsite.com or by simply yourwebsite.com, BUT NOT BY BOTH (without being 301 redirected).
Hope that helped. Happy programming!
EDIT: If you try renaming your about-us.php file to simply index.php, that would be your quick fix. Depending on your .htaccess configuration, I'm willing to bet that would be your easy fix.

Categories