htaccess pretty urls redirects the wrong way - php

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?

Related

How do I change the url look with .htaccess but keep the php functionality?

I'm trying to learn, so please be kind. My site is database driven and uses ID numbers to generate the pages. I'd like the URL not to show the ID number but the name of the page. Is there a way to do this where it is just cosmetic and doesn't effect the site? I'm also most likely screwing up the way I'm writing the redirect as it doesn't seem to work at all? What am I doing wrong? Thank you.
This is how my URL looks now:
http://mydomainname.com/index.php?id=35-Entertainment
I'd like it to look like this:
http://mydomainname.com/Entertainment
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [L,QSA]
Unfortunate there is not a way to do this.
The process in this case is the following:
A user enters the a url. In this case you want a nicely formatted URL (http://mydomainname.com/Entertainment)
The user sends this url to your server
Apache processes the url and points it to your project directory based on the domain used in the url
There Apache uses the htaccess to see what to do next. In this case the best method would most likely be to use RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] or something like that
Then you need to do the routing within your app. You can use a routing table for instance to map the nicely formatted slug to an module/id combination
This process can't be done by Apache for it does not known about the content present in your database.
Hopefully this is of any help for you.

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.

PHP - User Profile URL From Ugly URL

Alright, I usually try to figure things out on my own, but I am stumbled. I am currently making a PHP registration/login/profile system with PHP and SQL. I am 97% finished, but I would like some help with the users' profiles.
I currently have a form where you can search for users. This is a GET form that links to "www.example.com/profiles.php?username=." I would like for it so that I can link it to something like "www.example.com/user/" or "www.example.com/profile/." I would also like the functionality of going directly to the "user/" URL.
I am sure there are rewrite rules to do this, and I have tried many. I still seem to have trouble getting it to work. Any help? Thanks in advance.
Here is my current .htaccess file (doesn't work):
RewriteEngine On
RewriteRule ^user/([^/]*)\.php$ /profile.php?username=$1 [L]
If this htaccess does work, what should I be linking to? Because currently, linking to the "/profile.php?username=" doesn't do anything.
Again, to clarify, I want the output from the GET form to be able to link to www.example.com/profile/USERNAME or www.example.com/user/USERNAME. I would also like for the user to access their profile by typing www.example.com/profile/USERNAME.

Custom PHP URLs from Database

i was running into a problem while creating a simple database and i hope you can provide me with some help ;).
The problem :
I need to create a database which contains offers (for example for hotels, holidays etc) thats no problem at all. What we want to do is to make each entry customizable with a single url that can be chosen by the author itself. For example we've got an overview page (overview.php) where all the offers are listed. Now i want to click on the first offer "Mallorca" for example which inherits the id 50. The dynamic URL would be something like "detail.php?id=50 for example, but i want to rewrite them from a individual field in my database that the author specified while creating the entry which looks as following :
Offer Name
Offer Date
Offer Price
URL (which can be anything, also blabla.htm)
The URL field should then be used to create a custom individual url for each offer - for example detail.php?id=50 would become mydomain.com/offers/mallorca.htm
I know that mod_rewrite can be consulted but i haven't figured out how to combine it to use fields from my database.
Any advice?
Just ideas for You to think of or bounce off:
if You left a free hand to the author to enter the URL of their own, You would have to implement a controll for the URL uniqueness, while not letting the user to save the data until the URL part for the concrete offer is really unique
easier way is to only have the URLs like mydomain.com/offers/50/ - it still looks better while the implementation tooks less time
at the end, You would have to write a rewrite rule to Your .htaccess, while the result depends on what way You would go (from point 1 or point 2):
1: RewriteRule ^/offers/(.*)$ detail.php?url=$1 [L, QSA]
2: RewriteRule ^/offers/(.*)/$ detail.php?id=$1 [L, QSA]
If You'd go with the first option (the one You desired), You would also have to look up the offer by the URL part provided, in the second option You look up by the ID.
Hope this helps.
You could rewrite as follows:
RewriteRule ^/offers/(*.)$ detail.php?url=$1
You then have to find your offer in the detail.php file by it's url and the specified url GET parameter

Take Unique id and use as url for page

I am building a website for sharing links and pictures for php experience. Currently, each post has a page where users can comment on it. Every post also has an id that is stored in a database with it. What would be the easiest way to make this id the url of the post? For example:
For the question that has an id of 4a3cd5, I would want the url for that question to be post.com/posts.php/4a3cd5. thanks for your answers.
In the HTAccess page (.htaccess), write the following code:-
Options +FollowSymLinks
RewriteBase /
RewriteRule ^posts/(.*)/$ /posts.php?id=$1
So now, in the Address Space, if you write http://localhost/posts/4a3cd5, the user will be shown a page corresponding to the post ID of 4a3cd5. Internally, the URL which will get processed is this one http://localhost/posts.php?id=4a3cd5. This whole technique is being done by HTAccess, and this way of showing URLs to users is called SEF URLs.
More on the HTAccess tips & tricks can be found here.
Now in the page "posts.php", you can write all the logic which you want using the PHP GET Superglobal Array "$_GET['id']".
NP: A special note - please try to avoid this type of coding. Instead try using any of the available standard PHP MVC Frameworks.
That would involve a lot, maybe you should just have URL's like /posts.php/?post=4a3cd5.
Overall this would be much more practical. You can just GET the post variable and connect to your SQL database.

Categories