[Solved] Get current URL via PHP

Popular automatic website translation tool

[Solved] Get current URL via PHP

Postby Dawn » Sun Jan 06, 2013 2:42 am

Hi,

I'm using some php code to get the current active url and the current active uri. I need this to dynamicaly insert a canonical tag. It works great, but I've noticed that when I'm using your extension and access for example /en/ or /ru) it doesn't give me the right url's back. It returns the real url, but not the URL of the translation.

Example:
url shown in browser: http://www.domain.com/en/content?param1=1
return url: http://www.domain.com/content (and: http://www.domain.com/content?param1=1)
which is wrong and should return: http://www.domain.com/en/content (and http://www.domain.com/en/content?param1=1)

I use the following code:
Code: Select all
// Get SEO Page URL
$pageUrl = JURI::current();
// Get URI Object
$uri = & JFactory::getURI();
// Get URI String
$pageUri = $uri->toString();


I can deal with this myself, but can you please tell me how to get the real (SEO) url including your language "subdirectory" (by php - in this two variants)? Would be really important for me.

Best regards,
Dawn
Last edited by Dawn on Thu Jan 10, 2013 12:47 am, edited 1 time in total.
Dawn
 
Posts: 19
Joined: Sat Dec 22, 2012 11:20 pm

Re: Get current URL via PHP

Postby Edvard » Sun Jan 06, 2013 8:05 pm

Hi,

You can use JSession::get('glang'); to get the current selected language which will help you to generate the current URL.
Regards,

Edvard Ananyan - GTranslate Team

Please leave your feedback on your CMS plugin directory. It is very important for us!
Google Translate Joomla
Google Translate WordPress
Google Translate Drupal
Edvard
Site Admin
 
Posts: 4183
Joined: Mon Jun 28, 2010 1:54 pm
Location: Yerevan, Armenia

Re: Get current URL via PHP

Postby Dawn » Mon Jan 07, 2013 11:03 am

Hi Edward,

Thanks a lot! I think I can go with this solution.

Best regards,
Dawn
Dawn
 
Posts: 19
Joined: Sat Dec 22, 2012 11:20 pm

Re: [Solved]Get current URL via PHP

Postby Dawn » Thu Jan 10, 2013 12:46 am

Sorry to reopen this thread, perhaps it's a stupid question (Googled around too) but I'm not able to get anything. I've used the following code in my index.php:

Code: Select all
$test = JSession::get('glang');
echo($test);


Do I have to include something before? The other code I posted above works at exactly the same place, without any problems. Would be extremely nice from you if you can tell me how to get current selected language as string.

(Using Joomla 2.5)
Dawn
 
Posts: 19
Joined: Sat Dec 22, 2012 11:20 pm

Re: Get current URL via PHP

Postby Edvard » Thu Jan 10, 2013 8:15 pm

It should return empty string if there is no selected language, since you are not going to add default language code into the URL. After you select a language it will show the selected language.

As I understood you are using the plugin for joomla. If you have installed GTranslate Pro general, then you can read this thread: can-always-get-the-language-with-get-t2683.html
Regards,

Edvard Ananyan - GTranslate Team

Please leave your feedback on your CMS plugin directory. It is very important for us!
Google Translate Joomla
Google Translate WordPress
Google Translate Drupal
Edvard
Site Admin
 
Posts: 4183
Joined: Mon Jun 28, 2010 1:54 pm
Location: Yerevan, Armenia

Re: Get current URL via PHP

Postby Dawn » Fri Jan 11, 2013 4:11 am

Thanks for your answer. I also checked the linked thread. I've tested it with an url http://www.domain.com/en/ (my default language is german). In the index.php of my template in the header section I use:


Code: Select all
<?php

echo '<!--after this I should see the selected language gtranslate-->';
echo JSession::get('glang');

?>


As I understand you correctly this should output the string "en" - if I access for example http://www.domain.com/en/.

Sent you my URL via contact form.
Dawn
 
Posts: 19
Joined: Sat Dec 22, 2012 11:20 pm

Re: Get current URL via PHP

Postby Edvard » Fri Jan 11, 2013 7:18 pm

Hi,

I think you are using the general Pro version and have added the rewrite rules in .htaccess. Please check the mentioned thread and modify the rewrite rules, after that you will be able to access the selected language using $_GET['glang'] variable.
Regards,

Edvard Ananyan - GTranslate Team

Please leave your feedback on your CMS plugin directory. It is very important for us!
Google Translate Joomla
Google Translate WordPress
Google Translate Drupal
Edvard
Site Admin
 
Posts: 4183
Joined: Mon Jun 28, 2010 1:54 pm
Location: Yerevan, Armenia

Re: Get current URL via PHP

Postby Dawn » Fri Jan 11, 2013 7:21 pm

Sorry for not answering your questions related to version and .htaccess. I also didn't noticed that there is a changed .htaccess.

I'm currently using:
Code: Select all
# gtranslate config
RewriteRule ^([a-z]{2}|zh-CN|zh-TW)/([a-z]{2}|zh-CN|zh-TW)/(.*)$ /$1/$3 [R=301,L]
RewriteRule ^([a-z]{2}|zh-CN|zh-TW)/([a-z]{2}|zh-CN|zh-TW)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2}|zh-CN|zh-TW)/(.*)$ /gtranslate/translate.php?lang=$1&url=$2 [L,QSA]
RewriteRule ^([a-z]{2}|zh-CN|zh-TW)$ /gtranslate/translate.php?lang=$1 [L,QSA]


And yes I've got the pro version.

I'll try what you've got suggested and give you a feeback. Thanks for the fast answer!

EDIT: Saw next to the language related stuff there is a line remove. Do I don't need it with the modified version of the .htaccess?

Code: Select all
RewriteCond %{REQUEST_FILENAME} !-f
Dawn
 
Posts: 19
Joined: Sat Dec 22, 2012 11:20 pm

Re: Get current URL via PHP

Postby Edvard » Fri Jan 11, 2013 7:28 pm

It is OK to keep it.
Regards,

Edvard Ananyan - GTranslate Team

Please leave your feedback on your CMS plugin directory. It is very important for us!
Google Translate Joomla
Google Translate WordPress
Google Translate Drupal
Edvard
Site Admin
 
Posts: 4183
Joined: Mon Jun 28, 2010 1:54 pm
Location: Yerevan, Armenia

Re: Get current URL via PHP

Postby Dawn » Fri Jan 11, 2013 7:30 pm

It works as you said now! Thanks a lot :)
Dawn
 
Posts: 19
Joined: Sat Dec 22, 2012 11:20 pm

Re: Get current URL via PHP

Postby Edvard » Fri Jan 11, 2013 7:36 pm

Good to hear that! Thanks for your patience with us.
Regards,

Edvard Ananyan - GTranslate Team

Please leave your feedback on your CMS plugin directory. It is very important for us!
Google Translate Joomla
Google Translate WordPress
Google Translate Drupal
Edvard
Site Admin
 
Posts: 4183
Joined: Mon Jun 28, 2010 1:54 pm
Location: Yerevan, Armenia

Re: [Solved] Get current URL via PHP

Postby Dawn » Fri Jan 11, 2013 7:37 pm

Thanks for the good support - I really appreciate it :)
Dawn
 
Posts: 19
Joined: Sat Dec 22, 2012 11:20 pm


  • Related Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 0 guests

2GLux