Url Rewrite via.htaccess - php

I have a problem with Url Rewrite in my multilingual site, or better, maybe I don't understand it very well.
My original link is something like
www.mydomain.com/index.php?lang=en&s=2
I write this rule:
RewriteEngine On RewriteRule ^([^/]*)/([^/]*)\.html$ index.php?lang=$1&s=$2 [L]
and it's work. In fact if I write in the address bar
www.mydomain.com/en/2.html (smart url)
I go to the correct page with a smarter url on the address bar.
But the problem is that the link on my code is still:
link
and when I click it, the URL on the address bar is the original, not the smart one.
I thought that the rule on htaccess would have automatically "turned" (in the address bar) all my links in something smarter without changing the original php code...but maybe I'm wrong.
If I'm wrong how should I write my links in order to have smart urls but passing variables to identify languages and pages? I'm confused.
Thank you.

After rewriting URL in your .htaccess you need also to change the URL in your page for example in given link link you need to remove question mark
<a href="index.php/<?php echo $lang?>/2">

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 change requested filename

I need to do this using htaccess
When a request is made for http://www.example.com/home, it should (internally)load the page http://www.example.com/home_st.php
Similarly when a request is made to other link called products (http://www.example.com/products), it should (internally)load http://www.example.com/products_st.php
In short what it is doing is appending "_st.php" and loading that URL. But one thing I do not want is if the user directly types http://www.example.com/home_st.php or http://www.example.com/products_st.php in the browser, it should show 404 / Page not found error
I have few other pages in that folder and I want those pages to behave in this manner. I understand the htaccess should have something like this
Turn on the rewrite
Forbid access if the URL is called with page names like home_st.php, products_st.php etc.
If it's "home" or "products", then rewrite(append?) it to home_st.php and products_st.php respectively. I have other files too while need to follow the same
P.N: My URL should not show the actual filename, for example home_st.php, products_st.php etc. It should only show as http://www.example.com/home, http://www.example.com/products etc
htaccess and regex is not something that I am well acquainted with. Any help would be great. Thanks
You want to be able to re-write URL's
This has been written before but i'll say it again.
You want to use the Htaccess file and the re-write rule.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^pet-care/?$ pet_care_info_01_02_2008.php [NC,L] # Handle requests for "pet-care"
This will make this url: http://www.pets.com/pet_care_info_07_07_2008.php
Look like this: http://www.pets.com/pet-care/
Links to more information: How to make Clean URLs
and for the webpage I used to reference this information from: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
To get your pages set the way you want them, I would advise that you read the two articals and try get what you are looking for.
If you need some specific help with doing this, ask.
Hope that helps.

single .htaccess for multiple pages redirect with differnt url

i am changing my website and its old pages were like bellow
http://mydomain.com/keyword_city.html
while i read that its better to use - in page for batter SEO, so i am thinking to use new new page name like bellow
http://mydomain.com/keyword-city.html
first of all i want to know is this better idea to change page URL?
and if i change my URL does this effect on page ranking ?
i want to know how to write single .htaccess so that any request coming for old page will redirect to new URL, the only difference in old and new URL will be _ to -
Thanks
I think you are probably looking for:
Redirect 301 /keyword_city.html /keyword-city.html
to be placed in your .htaccess file.
As other people have mentioned though, probably not going to help a great deal when it comes to SEO. One thing that could however is using rich snippets/structured data:
https://support.google.com/webmasters/answer/99170?hl=en
Can be a lot of effort to do but would really help your presence on Google especially.
The following code added to .htacess should redirect all underscores _ to hyphens -
RewriteRule (.*)_(.*) $1-$2 [N]

htaccess is not working properly

I have a problem in htaccess file when i want to write a code of url rewriting in my htaccess file. I want to change this url "quotewebster.com/topics.php?topic_id=12" into this
"quotewebster.com/topics/12/"
I write this a code in my htaccess file:
RewriteRule ^topics/([0-9]+)/?$ topics.php?topic_id=$1 [NC,L]
And it is running fine, But a problem arises when i click any topic it goes to the right page and URL also rewrite but when i want to change the topic and again click on any topic then the url previous topic id embed with the new topic id and URL is something like that
"quotewebster.com/topics/12/topics/13/"
I don't understand it why it is happening. Please help me in this case. And sorry for bad english.
Thanks
Your problem is most likely with the way you generate your urls.
Make sure that your links start with a slash:
href="/topics/12"
not
href="topics/12"

URL rewriting problem

i have made a website in php.
There is a list of stories title stored in database and when user click any title among them then user is redirected to a page with a query string on it. like story.php?id=25
This means story with id 25 is now going to be displayed.
Now i want to rewrite URL but when i rewrite it there occurs a problem.
In story.php page i am reading the query string like $_GET['id'].. but after URL rewriting i am unable to read it like this. Can any body suggests what to do
You could use .htaccess to rewrite the long URLs server side, but not redirect the browser(so it still shows the long URL in the address bar), something like:
RewriteEngine on
RewriteRule story\/(\d+)\/(.+) story.php?id=$1
Just make you're long links look like www.site.com/story/25/This_is_the_title
If you made some adjustments to your url string you could do this.
http://www.domain.com/story.php?story=25&title=some_name
Which after re-write could be this.
http://www.domain.com/25/some_name.html
Code:
RewriteEngine On
RewriteRule ^story/([^/]*)/([^/]*)\.html$ /story.php?story=$1&title=$2 [L]

Categories