URL rewriting problem - php

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]

Related

.htaccess Mod Rewrite redirecting to 404

I am trying to create seo friendly url but after wasting alot of hours i have come here to ask for help. I am using xampp for windows.
This is what I want to achieve:
http://localhost/article/100/my-article-title/
Where 100 is my unique article id stored in the database.
This is my .htaccess rewrite rule:
# Rewriting the article URL
RewriteEngine On
RewriteRule ^article/([0-9]+)/([a-zA-Z\-]+)(/)?$ articlepage.php?id=$1&title=$2 [L]
This is my php page url format:
articlepage.php?id=100&title=my-article-title
When I try testing the seo friendly url above I get a 404 not found error.
PS. I was also curious about something else, if I remove question title from the stackoverflow url and press enter with only url id it redirects back to the url with question title. How deos it do this?

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 rewrite for multiple query string with one dynamic empty query string

So, I ran into an issue with query strings and rewriting them to seem "prettier"
My website is at: localhost/admin/ so when uploaded to my server it is also under the /admin directory, and the .htaccess file looks like this:
RewriteEngine On
# redirect URL with empty query string to index.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?(.*)$ /admin/index.php?$1 [NC,L]
Unfortunately, my design for the admin panel all of the pages are linked to the index.php file. So when I originally used to request http://localhost/admin/?users it would show a list of all of the users. I wanted to make this prettier so I used the above htaccess rules and came up with http://localhost/admin/users. Two issues arose with this:
The css files within the admin folder under css/ could not be accessed so none of the styles were being applied.
This ?users page contains pagination using the GET parameter in php as well, so it looks kind of like http://localhost/admin/users&page=1 and used to look like http://localhost/admin/?users&page=2.
So basically what I wanted to do was make the code look more like this: http://localhost/admin/users/page/1. Unfortunately I cannot hard code the users parameter in because I have other sections like "websites" and I will also have a edit users page with id's etc. So I was wondering if someone could help me with the best way to approach this problem or how I can fix the above htaccess rules? The only thing that kind of helped was: .htaccess mod_rewrite Unknown number of Variables of a GET form but I am still lost on how to implement it for multiple empty GET variables at the front of the url.
Thanks!
Here's what I do,
First for css part use <base href="domain.com/"> in head section of your pages.
You are redirecting full url to query string of index.php and I am assuming you are retrieving is as $_SERVER['QUERY_STRING'];.
So I suggest you that you change your pagination url to http://localhost/admin/users/page/1.
And after that when you retrieve them in index.php explode the string using explode function,
$arr = explode('/',$_SERVER['QUERY_STRING']);
Inside your $arr you will have all your values which you got them from query string.

Rewrite Old dynamic URL to new Dynamic URL

My website was using the below URL format
localhost/loc-New-Delhi-India (loc - was common in all URLs and the text after it used to change)
Now, I have changed it to
localhost/New-Delhi-India/location (Removed the loc and placed it at the end of URL as 'location'). For this, I'm using the below rewrite rule
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1
cityurl grabs the string after 'localhost' and searches the db.
Now since the website is heavily indexed by Google and shared a lot on social media, I want to redirect those urls to the new URLs. I tried the below code, but this just doesn't work.
RewriteEngine on
RewriteRule "loc-(.*)" "^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1" [R]
The new URLs are created successfully, but the old one's fail to redirect to the new one. I'm sure something's wrong but cannot find it. I researched the old questions but they are a bit different. Here, I'm not including any subdirectory after localhost and adding a directory 'localhost' after the city name.
Any help will be appreciated a lott :)
The following would probably work:
RewriteRule loc-(.*) $1/location [R=301,QSA]
RewriteRule ^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1
The first rule will trigger a 301 redirect which will also tell any search engines that your resource has been permanently moved and that would probably make them change their indexes to reflect this. Once you've redirected the 2nd rule should trigger as normal.

htaccess for dynamic web pages

I have a website and i am using MySQL to store and fetch data from, there is a bunch of data of different destinations (Yes this is a travel agent website) i am wondering how can i setup .htaccess file to display SEO friendly URL
For example: http://www.mywebsite.com/flights-result.php?id=10 this URL is a details page for a flight to Entebbe in Africa, i would like to have the URL for this like http://www.mywebsite.com/Africa/Entebbe.htm
And so on for them, one more thing do i need to add this for every page? the data is being update on daily basis so is there any easy way to write URL automatically?
Any help highly appreciated.
I don't really think what you are trying to accomplish has anything to do with mysql. What you are looking for is called URL rewriting. There are countless number of articles out there that could show you the direction to follow. I am not very sure which web server you are using right not. I presume it is Apache. Here is Apache module_rewrite guide.
Given the original URL, there isn't all the information in there to use mod_rewrite to do this completely.
So what you could do it send all web requests to a controller file, and from there parse the request uri and load the correct page.
So in htaccess, something like...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ controller.php [L]
Then in controller.php you parse the url and load the correct page.
A different option you may prefer, (if you're flexible on the specific final URL) is to have URLs ending up looking like this:
http://www.mywebsite.com/flights/10/Africa/Entebbe.htm
This would likely be simpler to do instead of implementing a controller (although I prefer the controller for routing requests).
So in htaccess...
RewriteRule
^/flights/([0-9]{1,10})/([a-zA-Z]+)/([a-zA-Z]+)\.htm$
flights-result.php?id=$1&country=$2&place=$3 [L]
Then near the start of the flights-results.php file you should load the data for the id, then check that the provided "country" and "place" are correct (to stop people just entering anything here), and return a 4040 if it's not.
Remember to change all the links your app outputs to the new style as well.
You could also, as you mentioned, hard code all these URLs into a htaccess, but that's not ideal :)

Categories