.htaccess Mod Rewrite redirecting to 404 - php

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?

Related

Redirect url of wordpress site on the basis of using htaccess

My website is powered by WordPress, and it now contains more than 95,000 articles.
The permalink that I'm using for this article right now is mywebsite.com/%sample-post%
I would like to add a custom URL such as mywebsite.com/article/%sample-post% to the website.
It is rather impossible to redirect url to new url one at a time, therefore what code will I place in htaccess that can redirect request url to new url? People are getting an error when they hit old weblinks from social media.
I tried in google and stakoverflow as well, didn't find solution
RewriteEngine On
RewriteCond %{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^(.*)$ https://%{HTTP_HOST}%article%{REQUEST_URI} [L,R=301]
I tried this

htaccess URL Rewriting issue redirecting to localhost/c:/ on XAMPP

I'm building a new version of a PHP app that has over 180k news items stored in the Database. Many contain hardcoded links in the news.body to the old file structure, which points to something like this people/7768-denis-grabe/ while the new file structure should be single-player.php?id=7768.
I'm really bad with REGEX and have very little understanding of how to work with .htaccess but I've been trying for two days to make a URL Rewrite rule which I think it's almost there but not quite yet.
In my .htaccess I have:
RewriteEngine On
RewriteRule ^people/([0-9]+)/?$ single-player.php?id=$1 [L]
It's not just rewriting the end of the URL, it's redirecting to the actual file, appending the http://localhost:c/xampp/... which gets me a 403 error forbidden page:
http://localhost/C:/xampp/htdocs/myapp/single-player.php?id=7768
All I need is to get the ID out of that first URL and rewrite it so it would redirect to the same address and just insert the ID as a GET request.
I'm working with XAMPP, mod_rewrite and AllowOverride All are already on
Thank you!
See if maybe this works?
RewriteRule ^people/([0-9]+).*$ single-player.php?id=$1 [L,NC]

Prevent users browsing the website with the old URL after rewriting

I am new to .htaccess, and I am trying to make friendly URLs (or pretty, SEO-friendly URLs). I did the following things on .htaccess:
- Remove .php extensions on all the file.
- Rewrite the product details pages as follow:
RewriteRule ^product-details/([a-zA-z0-9_-]+)/([0-9]+)$ product-details.php?title=$1&id=$2 [L]
However, when I go to the URL bar and type the file name with the .php extension, it still works. I want it to redirect the user to the 404.php page. Also, with the product details page, I type the old URL format, it also works.
Illustrations:
index => index.php (Works)
product-details/chair/1205 => product-details.php?title=chair&id=1205 (Works)
How to prevent this? Should I use PHP or .htaccess? If so, what is the .htaccess solution? Thanks a lot!
After RewriteRule, old URL's will automatically redirect to new URL's. Like if the user will open product-details.php?title=chair&id=1205 he will automatically redirect to product-details/chair/1205
So old .php extension URL's will never work even if someone will try to open them.
And I don't recommend to send old URL's to 404 page as this will make wrong impression on Users and not good for SEO.

How Do I redirect to php file with Htaccess

Is there a way to redierct a url to a php file based on one part of the url.
For example I have a url -
http://www.website.org/Cat/89/Diamond-Wedding-Rings.htm
I will have a few different of these urls. I wish to redirect all the pages with /Cat/ to a php file which will pull the Id and display the category and products.
in my htaccess I have
RewriteRule ^Cat/([0-9]+)/(.*)\.htm category.php?cat_id=$1&title=$2 [NC,L]
I'm not getting a 404 because the cms system redirects to the homepage if the page is not found.
I'm a complete noob when it comes to htaccess and I'm basing this on some of the existing rules in the htaccess which do something similar. I don't really need the title part it's really just the ID.
If all you need is the id then this will work
RewriteRule ^Cat/([0-9]+)/(.*) category.php?cat_id=$1

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