Making my php pages search engine friendly - php

In my site, I have used mod rewrite to make search engine and user friendly urls.
Only 3 rules:
RewriteRule ^articles/([a-z]+)/([0-9]+)/?$ /index.php?page=articles&cat=$1&id=$2 [L]
RewriteRule ^articles/([a-z]+)/?$ /index.php?page=articles&cat=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
But index.php is still accessible by anyone and will work even if a friendly URL is not used(that is, instead parameters are passed).
So, does this down rank by search engine ? Do I have to block direct access to files with .php extension ?

If you have 2 URLs that load the same page where one is search engine friendly and the other is not, this is not really detrimental to your site AFAIK. Basically you just want to expose to search engines as much as you can, so if you need to provide a parallel track, for example an anchor tag that works fine without Javascript because the action will take you to the correct place (which is ideal for a bot) but typically is managed by Javascript for clients that have it (most standard web browsers) then you're golden.
EDIT:
Per OP question in a comment about parallel paths.. Say I have a link, an anchor tag.
<a id="moxune_services" href="http://moxune.com/services" action="get" target="_self">Moxune Services</a>
You can see that this is a valid link (and I will be getting SEO points for it from StackOverflow ;P But anyway, say this is part of a heave JS driven site, and rather than refreshing the whole page when this link is clicked, I just want to have a subsection of the page like where a div w/ id="content" is present be replaced by the fresh content after I have AJAX load it. The js would be something like this (w/o testing, this is just off the top of my head) (a jQuery solution as well):
// very crude jQuery example!
$('#moxune_services').click(function() {
$.get($(this).attr('href'), function(sNewHtml) {
$('#content').replaceWith(sNewHtml);
});
});
Now you see, the google bot can reach the page through the HTML a tag, no problem, but your customers looking for a Web 2.0 (TM) website will be able to enjoy the lack of full page refreshes as they have JS enabled (and hopefully aren't using IE 6 :O).
One term for this is 'graceful degradation'.

quickshiftin is right here. There's no point hiding the index.php
If you must, however do this:
RewriteRule ^index\.php.+$ / [L,R=301]
I didn't test this, so it might not work, but the general idea is to redirect index.php to /

Related

How to differentiate between a url with and without parameters using .htaccess

I don't even know how I could title this question but here is the problem:
I use seo friendly urls on this project where there are services. Service links look like; .com/services/service-title-6 and they get translated to; .com/services.php?s=service-title-6. On the other hand, pages don't have the .php extension at the end of their urls like: .com/services or .com/about-us.
The actual problem comes when I'm handling the switch between multiple display languages for this website. I just used a get parameter called lang and I can pass it to any page since I have a php file I call on every page to handle this along with other stuff. On my navigation bar, the language change button links are created dynamically so that the user doesn't have to leave the current page to change the website's language. So if you are viewing the about page, the link is: .com/about-us?lang=en to switch to English. This is generated in php like $current_url . "?lang=en".
However, when I'm viewing a page with parameters such as a service, the link gets translated by .htaccess to something like: .com/services.php?s=service-title-6?lang=en. As you can tell, the 2nd question mark will get ignored as a parameter and not be passed. I would need to use an ampersand instead of the question mark. But when I'm just doing this on a regular page, I can't use ampersand by itself as it will be translated to: .com/about-us.php&lang=en.
Sorry for the long question but any help and ideas to handle this problem would be appreciated. Thanks.
I fixed my problem by appending the next query string to my url. I wasn't aware of the QSA flag of mod_rewrite.
Here is my original rule for the services page:
RewriteRule ^services/(.*?)/?$ services.php?s=$1 [L]
I replaced it with:
RewriteRule ^services/(.*?)/?$ services.php?s=$1&lang=$2 [L,QSA]
I don't know if it's 100% reliable but it seems to work perfectly for what I need, even after doing a few tests.
(Thanks to Joseph Sible-Reinstate Monica for directing me to a helpful question.)

.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]

Redirecting to main page (index)

I am building a website and I will need to make it SEO friendly so, as it is now, I am using a dynamic website (PHP) and through .htaccess, I am making it appear as if the site has static pages.
To do this, I am redirecting to a php file which then displays the content.
The url looks like: www.mainpage.dk/phpfile-navigationvalue-value.htm
I am using a navigation value inside the page to render it according to which menu item is clicked.
The guy I am building this for says that a url like www.mainpage/something.html is better for SEO purporses than www.mainpage.dk/phpfile-navigationvalue-value.htm. Can anyone come with some input on this matter?
And if the regular static page is better, is there a way to make a dynamic look just like a regular static page?
PS: The reason why I want a dynamic page is that the page is going to be extended with new pages every now and then as well as updated frequently.
To make it look like a regular page you could add *.html -> alias.php?alias=*
Then check the aliases and display the proper page from PHP.
Also, how about making it :
www.mainpage.dk/phpfile/navvalue/value/ -> index.php?page=phpfile&nav=navvalue&val=value
I would discourage redirecting to a phpfile, but handle it via index.php?page=* (look line above), or something similar.
Edit:
how htaccess should look
RewriteEngine on
# [!]for `*.html` -> `alias.php?alias=*`
RewriteRule ^(.*)\.html$ alias.php?alias=$1 [NC]
# [!]for `/phpfile/navvalue/value/` -> `index.php?page=phpfile&nav=navvalue&val=value`
RewriteRule ^(.*)/(.*)/(.*)(/)?$ index.php?page=$1&nav=$2&val=$3 [NC]

Very simple mod_rewrite questions

1- Does mod_rewrite means that if I make this url:
domain.com/ad.php?id=8498292
INTO
domain.com/8498292
that all links on my website will have to be changed to the later above?
example the link: domain.com/ad.php?id=8498292 wont work now, unless I replace it with domain.com/8498292 ?
Or will the server know that they are the same still?
2- Will the rewritten link appear rewritten in the browsers adress bars also, so if I enter domain.com/ad.php?id=8498292 it will actually appear as domain.com/8498292 in the adress bar itself?
3- Will images and all other related links and material on the page whose link is rewritten remain intact? ie will pictures and links still work FROM that page which are relative?
Thanks
You can write the rules such that both will work, but generally you'll want the links to be in the "clean" format for when search engines index your pages.
mod_rewrite can do an internal rewrite if the pages are on the same domain. One would have to use the [R] flag to force an external redirect if that was desired.
You can make the rules as expansive or as restricted as necessary in order to avoid rewriting media URLs. RewriteCond has a number of ways to test the viability of rewriting.

Adding dynamic content from a database

I'm using a new no-name framework that I'm not completely familiar with and I'm hung up on how I will add dynamic content from the database in this environment.
In the index controller, I'm forced to have an index function that answers all requests for index.html. If I wanted to create a second page dynamically from a data source, how do I go about this without having to add a controller for this new page? All requests are currently being routed by htaccess. Without getting into code snippets, I'm simply looking for the logic on how this is generally handled.
Okay, the question is a bit vague, so the answer might seem a little vague too.
Let's have an example to help visualise. You have a newspaper website, so you serve lots of articles - but all from one dynamic page...
/articles.do?Article=Important-News-Item
/articles.do?Article=Cash-Strapped-Business-Goes-Into-Administration
And so on.
You then add some .htaccess (mod_rewrite) love to the site...
/Articles/Important-News-Item/
/Articles/Cash-Strapped-Business-Goes-Into-Administration/
And you rewrite those URIs back to the articles.do page.
So in your articles.do page, you use the request string (Article=Value) to query the Article table and get back the relevant page...
SELECT
Headline,
Content,
Image
FROM
Article
WHERE
RestfulId = 'Important-News-Item'
And then you pop that onto the page.
Shout if I've missed the point on this question.
mod_rewrite example
This is the mod_rewrite rule for the example above, it maps the restful URI back to the articles.do URI.
RewriteEngine on
RewriteRule ^Articles/([^/\.]+)/?$ articles.do?Article=$1 [L,NC,QSA]
The easiest way to explain this (not the technical way to explain this) is:
1) The first bit (^Articles/) looks for any address that starts with "Articles/"
2) The second bit (([^/.]+)/) represents "Whatever comes between the "/" after Articles and the next "/" after that, which in this example is the title of the article.
You can capture more parameters by adding more "([^/.]+)/" segments to the end...
RewriteEngine on
RewriteRule ^Articles/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ articles.do?Article=$1&$2=$3 [L,NC,QSA]
In this example you could convert
/Articles/Important-News-Item/My-Key/My-Value/
into
articles.do?Article=Important-News-Item&My-Key=My-Value

Categories