Redirect Dynamic URL with one parameter - php

I have a url as mydomain.com/store.php?id=3.
I would like to convert(redirect) to static URL on the base of id.
Replace the ID with the store name (from database) and add some hard coated keyword at the end of url.
URL should be look like this one mydomain.com/storename-keyword1-keyword2.html
Please guide me that what is the best way to do this? I have used a lot of my time to handle this issue but no result.
Thanks

Are you trying to do something like this? How can I make my urls friendly from this example?
That is, you are still serving the content using the script store.php but you want the browser's location bar to have a clean URL that looks like /storename/word/word.html? You're obviously going to need to change some of the rewrite matches and targets to suit the URLs that you want. Something like?
RewriteRule ^storename-([^\-]+)-(.+).html /store.php?id=$1&something=$2&rewritten=true [L]

Related

htaccess pretty urls redirects the wrong way

I've look up for the answer to this question all around the web but I didn't find anyone with the same issue I have.
I want to create a sort of track-order like amazon or like any carrier does. To do that I assign a unique track-number to orders. Users can type the track-number into a form or directly into a url.
I get the track-number via the get method, so I need to prettify urls, but I also want to keep the search form functionality so I need a redirect.
I tried in this way:
the original url: /order/?reference=track-number where track_number is a random token like TRG58a8a132ec858.
the desired url: /order/track-number as stated above track_number is a random token like TRG58a8a132ec858.
in my .htaccess I wrote this rule:
Rewrite Engine on
RewriteRule ^/?order/([A-Z]{3}.*)/?$ /order/?reference=$1 [L,R=301]
I got some inspiration here, here, and here.
It works on reverse: when I type the original-url it does not redirect, when I type the desired-url instead it redirects to the original-url.
I am a little bit confused. Where I am making mistakes?

View url rules to get proper url

Take this link for example: http://example.com/p/12345
The 12345 part of the URL is a variable.
It should display like ?something=12345 but it doesnt so they are obviously using url rules/rematch. How do I view what the URL would be without the rules?
For example it might be something like http://example.com/p?number=12345
In a word, you can't. Unless you operate the web site and have access to the .htaccess file that is rewriting the URLs you can not determine exactly where it is being redirected.
In your example both the p and the 12345 could be variable.

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.

htaccess or php redirect to this task?

I need help from someone with experience :)
I have noticed that lot of sites with SEO friendly urls like www.site.com/123456/some-title only take the value 123456 and doesnt matter the rest, htaccess just send the variable 123456 to the specific url and then the visitor see the content.
So, if I type
www.site.com/123456/some-title-1
www.site.com/123456/some-title-2
www.site.com/123456/some-title-3
www.site.com/123456/some-title-4
I will always see the same page because the id passed is always the same 123456.
But I saw a few sites correcting this. For example:
-If I type any of the 4 urls above, they show me the content BUT they redirect me to the right url (www.site.com/123456/some-title) first.
How can I do that? I think this is a php question, since they need to connect to the database to check the original "url path" for the id 123456, right?
If someone can give me a tip to do that would be great!
Thanks
How can I do that? I think this is a php question, since they need to connect to the database to check the original "url path" for the id 123456, right?
This is a PHP question. 2 things need to happen here. When someone requests:
www.site.com/123456/some-title-1
There is routing that happens in htaccess, something like (example rules):
RewriteRule ^([0-9]+)/(.*)$ /some.php?id=$1&slug=$2 [L]
So whatever the script is (some.php), it'll get two parameers, an ID and a slug. The ID is what it uses to do a lookup into a database or wherever the actual content is stored. When it fetches that information from the database, it can compare slugs. If they differ, then instead of serving the content, redirect the browser to show the correct slug. Then we start back over from the beginning, browser submits request (this time with the right slug for the ID), rewrite rule routes it to some.php and because the slug is correct, the actual content gets served to the browser.

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