Magento: how to include anchor link in URL rewrite - php

I'm very new to coding and hope someone can help me.
On Magento Commerce 1.7 I'm running 3 stores (3 languages). I have added an anchor link (#example) to point to a certain position on our policies (CMS) page, which is available in all three stores in their respective languages.
However, each policies (CMS) page has it's own URL in it's language. To switch from one store to the other I've had to use the URL rewrite in Magento so as not to get a 404.
English site: www.example.com/en/policies/#example
German site: www.example.com/de/agb_s/#example
Dutch site: www.example.com/nl/algemene_voorwaarden/#example
However, the URL rewrite doesn't accept the #anchor symbol and just adding it at the end of the URL in the php code means it just disappears as soon as the rewrite kicks in.
<?php echo $this->getUrl('policies/') ?>#example"
Is there a way to write the anchor into the php or the URL rewrite so that it doesn't get lost?
Thank you in advance.

OK, found it.
In the URL rewrite in Magento backend the "#" anchor symbol in the request path is not allowed but the "#" anchor symbol is allowed in the target path.
So, in the phtml file where you have the link, instead of this code:
<a href="<?php echo $this->getUrl('policies')?>#example" >example</a>
change the code to:
<a href="<?php echo $this->getUrl('policies/example')?>" >example</a>
and in Magento redirect this URL to:
English store: policies/example -> policies/#example
German store: policies/example -> agb_s/#example
Dutch store: policies/example -> algemene_voorwaarden/#example
The URL "policies/example" doesn't exist but that doesn't matter. The URL rewrite will redirect it to your anchor on the store's respective policies page.
Might not be pretty but it works!

Related

a link in my website does not redirect to the specified path

">read More
I have the above link in my website news page so these links are supposed to redirect to the expanded news page. These links works well in my localhost however when i deployed my site in the server these links redirects to the index.php page.
As you can see the href attribute, it points towards "news_expanded" page but when i click on it, how does it redirect me to the index.php file ?
You can add a slash (/) in front of your URL. That should fix the problem.
It is very important to start URLs with / when you are going to change structure of you site.
For example, if you are on /test page and then you have link on this page is testing. When you click on this link it will redirect to /test/testing instead of /testing. So that means it will consider as child page of current page. So forward slash intended URL is returned instead of its parent or child.
So it is good practice to all forward slash in all links or you can specify you site URL as a constant and add this constant value like below:
Define constant in common file like this define('SITE_URL','http://example.com/');
Use like below
<a class="read_more_wrapper" href="<?php echo SITE_URL; ?>news_expanded.php?news_id=<?php echo $record['news_id']; ?>"><button class="read-more">read More</button></a>
Or just add forward slash like this
<a class="read_more_wrapper" href="/news_expanded.php?news_id=<?php echo $record['news_id']; ?>"><button class="read-more">read More</button></a>

how to use geturl with an index.php in magento?

the title may seem not clear. now this is my issues.
im using windows which is i need to run the magento with "index.php" on it.
example : "localhost/magentoproject/index.php/test/index/save"
now im using the getUrl function on form like this
<?php echo Mage::getUrl('test/index/save')?>
but after i inspect, the url shows like this
localhost/magentoproject/test/index/save
the url appear is not including the index.php, therefore, magento shows 404 not found.
how i solve this problem ? how to use geturl including the "index.php" on it.
currently im use the hardcode like this on the form
http://localhost/magentoproject/index.php/test/index/save
Go to admin url of your site.
Next go to System > configuration > web > Search Engines Optimization
Then set Use Web Server Rewrites value to 'No'

Make a link redirect to the same page

I've created a page with an article. On top of that there's a title. If people try to click this title I want them to be redirected to the same page.
Like this: https://gyazo.com/74350b4fe91c670c4101449ee1c928a4 If I click on the article it just refreshes the page.
I can't do this manually for every article because I'm using a script.
The code I've written looks like this:
echo ' <h1 class="entry-title">'.$row['postTitle'].'</h1>';
As you can see I wrote
<a href="/">
and that's will not redirect me / refresh the page i'm viewing.
This is how it looks for me: https://gyazo.com/8a15ae274d8a7240b07100395460568d
as you can see it does not redirect me to the same page when i click the title.
How can I do this?
To make your custom PHP blog post template be able to display a title link that points back to the page, one way is to make use of your $row[.... variable.
Provided that,
your URLS will look like your screenshots, such as http://localhost/viewpost.php?id=8 when running locally and for example http://www.yourwebsite.com/viewpost.php?id=8 when online
you know how to refer to the post's id that is used in the ...viewpost.php?id=8, for example $row['postID']
you don't yet have any variable or means to refer to your current domain http://localhost when local or http://www.yourwebsite.com when online
Then, I recommend a two-part approach:
Somewhere at the top of your code, or perhaps in an include you might use for such code-reuse purposes, define for example $host:
$host='http://' . $_SERVER['SERVER_NAME'];
Then, for your actual title link::
echo ' <h1 class="entry-title">' . $row['postTitle'] . '</h1>';
Explanation
Separated HTML from the concatenation dots . with spaces, to be easier to read, as well as to support any helper programs such as fmt that you might use for wrapping long lines, so they have spaces to use for wrapping lines.
Uses PHP's predefined $_SERVER variable's SERVER_NAME , which, combined with the http://, the $host will be http://localhost when local and http://www.yourwebsite.com when online.
Define $host as a variable once at the top of the page, because it is clearer that way and likely you will have a use for it elsewhere on the page, so this helps avoid having to repeat yourself writing 'http://'.$_SERVER['SERVER_NAME'] everywhere else you may need to start forming the absolute URL
$host is then combined with the pattern for the rest of the URL, to assemble the absolute URL
Absolute URL is helpful so that if a user saves your article to their computer, then later clicks the title link on their locally saved article, the user can still correctly reach the original online page
As the article author, setting a link this way also means it can serve as a permalink, which helps with search engine optimization (SEO)
Please use # in href attribute! that will redirect you on the same page.
Problem solved. I used
echo '<h1>'.$row['postTitle'].'</h1>';
Thank you everyone.

CSS Menu URL's not directing appropriately

I created a CSS menu, to use on a PHP site, but now I realised that for example:
From the Home tab (URL localhost/site), all the item links are correct if you hover over them, but now i navigate to "Stock" for example, who's URL is localhost/site/stock... it opens correctly. So from Stock i want to navigate to Sales (localhost/site/sales), the URL of sales become localhost/site/stock/sales and not just localhost/site/sales.
I added this menu only recently, the previous one was working fine, so for a test, i replaced the css menu with the previous one, but to no avail. the problem still persist, so I assume that something else must have changed the behaviour of the links...
I can probably fix this by added " ../ " in front of the menu's URL, but on the other hand. not all items are just one step back.
Your help would be greatly appreciated.
Try using absolute URLs.
When your link is on a page located at localhost/site/stock:
Link <!-- goes to localhost/site/stock/sales -->
But if you prepend a forward slash, the destination will be absolute, relative to your document root:
Link <!-- goes to localhost/site/sales -->
Assuming of course that your document root is localhost/site
More likely your links will have to look something like:
Link
You're correct in saying you need to add slashes. My suggestion would be to provide absolute links from / to your page:
/site
/site/sales
/site/sales/sale24
/site/stock
/site/stock/secondstockpage
/site/stock/thirdpage
I have changed the menu as you suggested, but now, it works as follow:
if i for example set my menu link to direct to: /site/sales/sold.php
so the menu.php file link would look like this:
<li>Sales</li>
<ul>
<li>Sold Items</li>
</ul>
it would sometime direct to /site/sales/sold.php and the next moment, it repeats the menu's directory like: /site/sales/sales/sold.php
so i removed the sales directory, as its directing there by itself, worked for a little while and now it directs to /site/sold.php which does not exist so it ends up with a 404.
should i specify a siteroute somewhere else?
this is very weird to me. first time i encounter something like this!
Hope my examples make sense!

Apache mod_rewrite syntax not working

OK guys, I have a problem that is literally driving me crazy. Here's what happened :
I decided that I wanted to rewrite the URL on my web-site.
It is supposed to rewrite from this syntax :
http://www.sample.com/programming.php?name=something
to this :
http://www.sample.com/tutorials/programming/something.php
Or (eg. 2) :
http://www.sample.com/other.php?name=test
to this :
http://www.sample.com/tutorials/other/test.php
So my URL syntax would be :
http://www.sample.com/tutorials/(name of my file)/(name of the variable).php
I have tried the following code :
RewriteEngine On
RewriteRule ^tutorials/programming/(.+)$ /programming.php?name=$1 [L]
RewriteRule ^tutorials/other/(.+)$ /other.php?name=$1 [L]
But, it doesn't rewrite the URL properly. The detailed explanation is below :
So, when I visit my original site, it appears like this :
http://www.sample.com/programming.php?name=something
If I visit this URL :
http://www.sample.com/tutorials/programming/something.php
I get my web-site HTML, but without my CSS layout (just HTML displayed). Also, if I click on any other link on non-CSS site, I get error 404. Note that the URL for the index.php site isn't as it's supposed to be :
http://www.something.com/index.php (Correct index.php URL)
but it's like this :
http://www.sample.com/tutorials/programming/index.php (which does not exist).
I have read over 10 tutorials online, asked my colleague to help me out, but neither did his solutions work. So, all I want to accomplish is that my URL is rewritten, so when the user choose a tutorial in programming, I don't get this URL in the address bar :
http://www.sample.com/programming.php?name=something
but this :
http://www.sample.com/tutorials/programming/something.php
and that is all I want.
I have tried to be as detailed as possible. If you need additional details, please, let me know.
Thanks in advance!
I get my web-site HTML, but without my CSS layout (just HTML displayed). Also, if I click on any other link on non-CSS site, I get error 404. Note that the URL for the index.php site isn't as it's supposed to be :
The relative/absolute paths you have in your page content is getting a different base because of the extra slash. When you have something like <img src="images/blah.gif">, the relative base is derived from the URL the browser sees (not what is internally re-written by the server). So if the browser sees: http://www.sample.com/programming.php?name=something, the URI is /programming.php and the URI base is /. But if the browser sees http://www.sample.com/tutorials/programming/something.php, the URI is /tutorials/programming/something.php and the URI base becomes /tutorials/programming/, which I'm assuming is not where your images/css/scripts/etc are located (since that directory probably doesn't even exist).
You need to either correct the URI base in all of your page headers by adding a:
<base href="/">
Or change all of your relative links to absolute links.
I get my web-site HTML, but without my CSS layout (just HTML displayed)
how did you include the css? did you include your files in the way
../../css/file.css
or in absolute mode
/css/file.css?
to check what really is the fault (i guess your rewrite success in your task eg i didn't understand it the right way) can you give us the real uri's?

Categories