Sorry if the title doesn't sound clear but basically, I see multiple APIs doing it like this:
http://www.website.com/183718
Where 183718 would be the ID and then they could use that to pull results.
I want to do the same but I don't know another way besides like this:
http://www.website.com/id?=183718
How could I accomplish this?
You want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
And this maps requests from
/news.php?news_id=63
to
/news/63.html
Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:
<Files news>
ForceType application/x-httpd-php
</Files>
And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:
<?php
echo $_SERVER['PATH_INFO'];
// outputs '/63.html'
?>
Related
Help Me to remove (.php) extension
I have an index page (index.php) that has some hyperlinks that goes to project_details.php?name=$name.
So the current link is like (http://localhost/development/project_details.php?name=$name).
Now I want to change the url to (http://localhost/development/$name) and then in project_dtails.php, I want to get the name.
How shall i do this?
You can do this by using .htaccess .
Options -Multiviews
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-/]+)$ project_details.php?name=$1
Now you can access the parameter(name) like this in project_details.php file
echo $_GET['name'];
A solution to do something like this is to create a router wich will matchs some URL and include the right files based on that. For example if the project name is myProject you can have http://localhost/development/myProject.
There is an example here : https://www.taniarascia.com/the-simplest-php-router/
I hope I have been helpful.
I have the following htaccess file:
Rewriterule ^view_profile-(.*)$ view.php?user=$1
When I do something like "View profile it works great (URL looks like: http://home.com/view_profile-John). However, if I try this:
Rewriterule ^view/profile-(.*)$ view.php?id=$1 it says that page cannot be found.
Does anyone know why is not working?
You probably need to turn off Multiviews here. Because the beginning of the request looks like /view/... and there's a file /view.php, mod_negotiation will automatically assume you mean the php file and route it there before mod_rewrite even gets a chance to do anything. Try adding:
Options -Multiviews
I'm working on a website with a lot of profiles. What I want is that you can simply navigate to one of those profiles with a URL like www.mywebsite.com/userX (just like twitter with twitter.com/userX).
I already know how this is possible by using the .htaccess and rewriterule, but the problem is it changes the entirely URL to something like www.mywebsite.com/?username=userX. It must just load a specific PHP-file without rewriting the URL in the addressbar.
Who knows how I can perform this?
(p.s. sorry for my average English)
See the fine article:
http://www.phpriot.com/articles/search-engine-urls
It explains that yeah, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
And this maps requests from
/news.php?news_id=63
to
/news/63.html
Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:
<Files news>
ForceType application/x-httpd-php
</Files>
And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:
<?php
echo $_SERVER['PATH_INFO'];
// outputs '/63.html'
?>
My urls currently look like this:
http://domain.com/news/articles.php?id=22&category=investments&title=securing-your-future-making-the-right-investment
How can I use mod_rewrite to make the url look more like this:
http://domain.com/news/articles/investments/securing-your-future-making-the-right-investment
EDIT: need to include the id variable too
#enable mod rewrite
RewriteEngine On
RewriteRule ^/news/articles.php?id=([0-9]+)&category=([a-zA-Z]+)&title=([a-zA-Z]+)$ /news/articles/$2/$1_$3
the ID must exist in the URL so the it looks like:
http://domain.com/news/articles/investments/{ID}_securing-your-future-making-the-right-investment
Good luck.
Add something like this to your .htaccess file:
^/news/articles/([0-9]+)/(.*)$securing-your-future-making-the-right-investment$
/news/articles.php?id=$1&category=$3s&title=$2 [L]
Don't know about the $3, but category doesn't seem to be present in the url you listed so I guess it isnt needed.
This should make it work ;)
I have a multiple language website, and I use a php get variable to set the cookie for the language setting. I have multiple subfolders (http://www.site.com/es and http://www.site.com/de) that each have a respective .htaccess file. When accessing these folders, the .htaccess file does this to "silently" redirect the user and add the appropriate php variable:
-------
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
rewriterule ^http://www.site.com/es/$ http://www.site.com/?l=es [P,R=301]
rewriterule ^(.*)$ http://www.site.com/$1?l=es [P,R=301]
-------
When someone accesses the root directory: http://www.site.com, I want to add a ?l=en suffix "silently" to the url. How do I do that? Thanks.
I don't think it's possible in the method you want.
However, you could (in a round about way) modify your code:
RewritRule ... proxy_add_l.php
In which, the code would:
<?php
$_GET['l'] = $_REQUEST['l'] = 'en';
require 'index.php';
?>
Note this is really, really, bad, and should only be used as a last resort really.
Can't you just redirect them to /en which looks cleaner?
I think if you want a fixed value this is perfect possible with something like this:
rewriterule ^http://www.site.com/$ http://www.site.com/?l=en ...
I cant test the result cause am not at home now.