htaccess - Friendly url with redirection - php

I have been looking for several solutions on the web but as I have always had problems interpreting the htaccess, I have not achieved what I wanted.
I have a website with the following type of link:
https://sub.example.com/search.php?gosearch=new+music+techno
https://sub.example.com/search.php?gosearch=nice-music-rock
And I would like to obtain a link in this way:
https://sub.example.com/search/new-music-techno
I also have a problem, when in my form I press enter, this automatically adds the "+" symbol to all the spaces and I do not know with what method the change would be made automatically to a "-", if with PHP, with JQuery or with .htacess.
In parallel I also have this type of links:
https://sub.example.com/topmusic.php
And I would like to obtain this type of link:
https://sub.example.com/topmusic/
Is it possible to implement everything with htacess?
Do I need to make several conditions or just one?
Is it possible that old urls are redirected automatically to the new form once the user clicks on them?

You can use like this
RewriteRule ^search/(.*)$ search.php?gosearch=$1 [NC,L,QSA]
And on search.php you need to replace the code. htaccess will send the whole string next to "search/" on search.php file under gosearch $_GET parameter.

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]

PHP SEO optimized address and physical page address conflict

I have a static website that I am planning to move towards php. I will be using php to just load the navigation.htm and main contents page and combine to form a single html file.
Actual Page is here: http://abubakar.zxq.net/html/0007HorizontalAxisWindTurbineMadeWithPVCPipe.htm
How It is accessed through php:
http://abubakar.zxq.net/index.php?page=0007HorizontalAxisWindTurbineMadeWithPVCPipe.htm
I want the page to look like 2 when site is addressed with URL in 1. It is needed to keep the already shared URLs remain working.
Here is how I defined httaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^html/([a-zA-Z0-9_-]+)$ index.php?page=$1
Kindly inform is it possible to do it in present form? I have a solution in mind that old files should be shifted to a new folder and the design php so that it works on old link. Thanks in advance for your kind help.
The regex you wrote doesn't match the first URL.
You need to add the dot character (because the URL ends up with ".htm"), try this :
RewriteRule ^html/([a-zA-Z0-9_\.-]+)$ index.php?page=$1

Removing query string from url in php

Lest say i have a link and when some one click on that link it redirect the person to the following page
http://localhost/install.php?act=go
now i just want to remove everything after ? so that person doesn't see the actual query string behind it or is there any way to encode the characters and make it look different in browser
You can add a mod_rewrite rule to your htaccess something like
RewriteRule ^install/([A-Za-z0-9-_]+)$ /install.php?act=$1
that way /install/go takes you to the destinantion

Categories