I have a semi-professional website about my travels where I present all the pictures for each country.
For that I have a MySQL-Database containing the following attributes:
- country
- country-id
- pictures (links)
I am using a PHP-file which requests the pictures: "country.php"
So I get the URL-Structure: www.url.com/country/spain/ or www.url.com/country/usa/.
What I need is to change the URL Structure to www.url.com/spain but still maintain that "central" country.php-file.
I know a method which only works, when I have a folder for each Country in my webspace and an index.php-file which simply loads the country.php-file (I did that on another website). But in this case I would have way too many folders, which makes it all too complex since there are not only countries but also "special places and cities".
Is there another way to do that?
EDIT:
I got it with this code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ country.php?url=$1
But now the problem is, that whatever I type in, eg. www.url.com/xyz, the server thinks that I want to open the country "xyz". What can I do, to only rewrite, when the entered country is in my database?.
I am sorry if I ask too complicated, but I just didn't know how explain my problem better.
If you dont have any framework in use, which handles your routing, you could simply use the .htaccess file and set a rewrite rule.
Look here for example: how to remove folder name from url using htaccess
$url ="******YOUR URL HERE***********";
$urlArr = parse_url($url);
$urlbase = $urlArr['host'] . #$urlArr['path'];
Related
I want to make user-friendly API for my service. I am trying to make it like Discord API - https://example.com/api/accounts/<account ID>/<stuff>/, some expected link examples below:
https://example.com/api/accounts/1234567890/data/
https://example.com/api/accounts/1234567890/inventory/
https://example.com/api/accounts/1234567890/servers/
I tried to make some htaccess tricks, but without any result - website got corrupted (just white page without anything) or account ID was considered as folder (HTTP 404, because I don't want those IDs as folders).
Thank you in advance for your response
Edit 1:
File structure can be really any, I just want it working. But most expected by me would be
api
- accounts
- data
- inventory
- ...
- server (example)
- game (example)
Edit 2:
My API is OOP - so I can call it from any place. I just need to get data and create new object and call some functions. In my bad-looking API, link was like: https://example.com/api/accounts/data.php?id=123456789 and in that script I call $stuff = new StuffObj($id, $other, $stuff, $from, $post); and function like $stuff->execute();. But with new API I can just make GET var, like action and in switch choose what object to use.
In my bad-looking API, link was like: https://example.com/api/accounts/data.php?id=123456789
In that case, to implement a "pretty" URL of the form /api/accounts/<id>/data/, you would do something like the following using mod_rewrite in your /api/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteRule ^accounts/(\d+)/data/$ accounts/data.php?id=$1 [L]
To make this more "generic", to rewrite a URL of the form /api/accounts/<id>/<action>/ to /api/accounts/<action>.php?id=<id>, assuming <action> would only consist of lowercase letters then you could change the above RewriteRule to read:
RewriteRule ^accounts/(\d+)/([a-z]+)/$ accounts/$2.php?id=$1 [L]
Although in your file structure you appear to have "subdirectories"(?) of the /accounts directory that also match the basename of the .php file? If so, this could be problematic, hence why I have disabled MutliViews above.
But with new API I can just make GET var, like action
Yes, you could - and would perhaps be preferable - everything is managed by a single entry point (eg. index.php - but you can call it anything your like) - However, you need to be specific and decide on this before implementing the "pretty URL" - which is really just cosmetic fluff. In fact, if this is just an API, do you need to implement a "pretty URL" at all?
My .htaccess works (not an expert, but do a lot of copy & paste).
A typical rewrite for one of my pages looks like this:
RewriteRule ^my-grills-parts-and-service/?$ parts-service.php [NC,L]
...where the url "http://www.mydomain.com/my-grills-parts-and-service/" will invoke the parts-service.php file.
However, I am using php to construct a fully qualified URL, from what is in the address bar. The problem is, the server variable $_SERVER['SCRIPT_NAME'] will return "parts-service.php", rather than what is actually in the address bar, "my-grills-parts-and-service/".
I want php to somehow read the literal rewrite string, rather than the actual filename.
So, I want php to construct a string like this:
$url = "http://www.mydomain.com/my-grills-parts-and-service/";
I hope I don't have to write a data table or switch or if block, because that would be doing everything twice (once in the .htaccess, and again in a php file).
You can use $_SERVER['REQUEST_URI'] to get real url address.
Before using this method, check #regilero comment.
I have a website that responds on *.domain.com.
Going to x.domain.com or y.domain.com should produce the same web page.
What * is I do not know, but it is and important piece of info since we track things based on it.
When moving to wordpress we ran into a pretty severe problem. It seems to generate links (using get_page_link) with the domain which is set in the admin.
This will not work for us because we can't find a way to tell wordpress to generate links without the domain (why does it do this anyway?!) and every time a link is clicked the browser goes from: x.domain.com to domain.com (since domain.com is what we have in the admin).
Unfortunately WordPress is architected such that it is really hard to get rid of the domain component of URLs. But all is not lost! Read on as the answer to your question requires a bit of background.
The WordPress team made the decision to require the user of a site to hardcode the site domain either in the database via an admin console, which you can see in the following screen shot, of via PHP which we'll discuss below:
You might ask what the difference is between the the two URLs? Even I find it confusing because I almost never need anything other them to have them both set to the root URL and as it's not important for your question I'll just gloss over that detail. If you are interested though you can learn more here:
Changing The Site URL
Editing wp-config.php: WordPress Address (URL)
Giving WordPress Its Own Directory
Moving along, the other option is to hardcode the two PHP constants WP_SITEURL and WP_HOME in the /wp-config.php file which can be found in the root of a WordPress installation. Those two lines might look like this in your /wp-config.php file:
define('WP_HOST','http://domain.com');
define('WP_SITEURL','http://domain.com');
The good news is that you can define both of them dynamically based on the current domain your site is serving from (I'm going to assume you have both your DNS server and your Apache web server configured for wildcard DNS.) You can use the following code to match any subdomain name consisting of letters and numbers:
$root_domain = 'domain.com'; // Be sure to set this to your 2nd level domain!!!
$this_domain = $_SERVER['SERVER_NAME'];
if (!preg_match("#^([a-zA-Z0-9]+\.)?{$root_domain}$#",$this_domain)) {
echo "ERROR: The domain [$this_domain] is not a valid domain for this website.";
die();
} else {
define('WP_HOME',"http://{$this_domain}");
define('WP_SITEURL',"http://{$this_domain}");
}
The bad news is you may have some "artifacts" to deal with after you get it working such as how URLs are handled for image URLs stored in the database content (which may or may not end up being a problem) or for Google Maps API keys, etc. If you have trouble with them let me suggest you post another question here or even better at the new WordPress Answers Exchange also run by the same people as StackOverflow.
As for telling WordPress how to generate links, there are filters you can "hook" but in my quick testing I don't think you need it because WordPress will generate the links for whatever domain happens to be your current domain. Still if you do find you need them you can do it although be prepared to be overwhelmed by all the add_filter() statements required! Each one controls one of the different ways links can be generated in WordPress.
Here is the hook filter function and the 40+ add_filter() calls; you might not need them all but if you do here they are:
function multi_subdomain_permalink($permalink){
$root_domain = 'domain.com';
$this_domain = $_SERVER['SERVER_NAME'];
if (preg_match("#^([a-zA-Z0-9]+)\.?{$root_domain}$#",$this_domain,$match)) {
$permalink = str_replace("http://{$match[1]}.",'http://',$permalink);
}
return $permalink;
}
add_filter('page_link','multi_subdomain_permalink');
add_filter('post_link','multi_subdomain_permalink');
add_filter('term_link','multi_subdomain_permalink');
add_filter('tag_link','multi_subdomain_permalink');
add_filter('category_link','multi_subdomain_permalink');
add_filter('post_type_link','multi_subdomain_permalink');
add_filter('attachment_link','multi_subdomain_permalink');
add_filter('year_link','multi_subdomain_permalink');
add_filter('month_link','multi_subdomain_permalink');
add_filter('day_link','multi_subdomain_permalink');
add_filter('search_link','multi_subdomain_permalink');
add_filter('feed_link','multi_subdomain_permalink');
add_filter('post_comments_feed_link','multi_subdomain_permalink');
add_filter('author_feed_link','multi_subdomain_permalink');
add_filter('category_feed_link','multi_subdomain_permalink');
add_filter('taxonomy_feed_link','multi_subdomain_permalink');
add_filter('search_feed_link','multi_subdomain_permalink');
add_filter('get_edit_tag_link','multi_subdomain_permalink');
add_filter('get_edit_post_link','multi_subdomain_permalink');
add_filter('get_delete_post_link','multi_subdomain_permalink');
add_filter('get_edit_comment_link','multi_subdomain_permalink');
add_filter('get_edit_bookmark_link','multi_subdomain_permalink');
add_filter('index_rel_link','multi_subdomain_permalink');
add_filter('parent_post_rel_link','multi_subdomain_permalink');
add_filter('previous_post_rel_link','multi_subdomain_permalink');
add_filter('next_post_rel_link','multi_subdomain_permalink');
add_filter('start_post_rel_link','multi_subdomain_permalink');
add_filter('end_post_rel_link','multi_subdomain_permalink');
add_filter('previous_post_link','multi_subdomain_permalink');
add_filter('next_post_link','multi_subdomain_permalink');
add_filter('get_pagenum_link','multi_subdomain_permalink');
add_filter('get_comments_pagenum_link','multi_subdomain_permalink');
add_filter('shortcut_link','multi_subdomain_permalink');
add_filter('get_shortlink','multi_subdomain_permalink');
add_filter('home_url','multi_subdomain_permalink');
add_filter('site_url','multi_subdomain_permalink');
add_filter('admin_url','multi_subdomain_permalink');
add_filter('includes_url','multi_subdomain_permalink');
add_filter('content_url','multi_subdomain_permalink');
add_filter('plugins_url','multi_subdomain_permalink');
add_filter('network_site_url','multi_subdomain_permalink');
add_filter('network_home_url','multi_subdomain_permalink');
add_filter('network_admin_url','multi_subdomain_permalink');
While brings us to the final point. There is functionality in WordPress that attempts to ensure every URL that is loaded is served via its canonical URL which in general is a web best practice, especially if you are concerned with optimizing search engine results on Google and other search engines. In your case, however, if you really do not want WordPress to redirect to your canonical URL then you need to add a redirect_canonical filter hook and tell WordPress not to do it.
What follows is the code to make sure any page that serves as "x.domain.com" stays on "x.domain.com" even if all the URLs are filtered to be "domain.com". That may not be the exact logic you need but I'm just showing you the building blocks of WordPress so you'll be able to figure out the logic that you require.
A few final details about this function call; parameters #3 and #4 refer respectively to the priority (10 is standard priority so this hook will not be handled special) and the number of function arguments (the 2 arguments are $redirect_url and $requested_url.) The other thing to note is that returning false instead of a valid URL cancels the canonical redirect:
add_filter('redirect_canonical','multi_subdomain_redirect_canonical',10,2);
function multi_subdomain_redirect_canonical($redirect_url,$requested_url){
$redirect = parse_url($redirect_url);
$requested = parse_url($requested_url);
// If the path+query is the same for both URLs, Requested and Redirect, and
if ($redirect['path']+$redirect['query']==$requested['path']+$requested['query']) {
// If Requested URL is a subdomain of the Redirect URL
if (preg_match("#^([a-zA-Z0-9]+).{$redirect['host']}$#",$requested['host'])) {
$redirect_url = false; // Then cancel the redirect
}
}
return $redirect_url;
}
That's about it. Hope this helps.
-Mike
do you have any control over your hosting? Maybe you can use the rewrite module in apache, if you are using apache.
In httpd.conf add:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^x\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/x/$1
RewriteCond %{HTTP_HOST} ^y\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/y/$1
You can change the way you pass the variable "v" too
RewriteRule ^(.*)$ http://www.domain.com/$1&var=v
I didn't try the code, but i'm pretty sure that at least it will open your mind to a new way to deal with this issue --one that doesn't involve so much coding.
Cheers.
A.
this is the URL path I currently use:
/index.php?page=1&title=articles
I want to get the URL path as
/index/page-1/title-articles
using SEO user friendly URLs in PHP.
And how to get the value of the "title"? Any one can help me pls.
Check out the mod_rewrite module, and maybe for a good starting point, this tutorial on how to take advantage of it with PHP.
You need to ensure two things:
your application prints out the new URLs properly, and
your webserver can understand that new URLs and rewrites them to your internal scheme or redirects them back to your application and your application does the rest.
The first part can be simply accomplished by using
echo ' … ';
instead of
echo ' … ';
The second part can be accomplished either with URl mapping features of your webserver (most webservers have a module like Apache’s mod_rewrite). With mod_rewrite, the following will do the rewrite:
RewriteEngine on
RewriteRule ^index/([^/-]+)-([^/]+)(.*) /index$3?$1=$2 [N,QSA]
RewriteRule ^index$ index.php [L]
The first rule will extract one parameter at a time and append it to the query. The second rule will finally rewrite the remaining /index URL path to /index.php.
I want to get the URL path as
/index/page-1/title-articles
Why? I’ve got two objections:
index is a no-information and I doubt that it belongs in the URI
page-1, as well as title-articles looks plain weird. Like with index, you should ask yourself whether this information belongs here. If it does, make clear that it’s the key of a key-value pair. Or remove it entirely.
Thus, I propose either:
/‹article›/1
or
/‹article›/page/1
or
/‹article›/page=1
or
/‹article›[1]
or
/articles/‹article›/page/1
Or any combination thereof. (In the above, ‹article› is a placeholder for the real title, the other words are literals).
I'm trying to put something with this, whenever I go to a page like:
http://www.example.com/character.php?id=3
I want the mod rewrite to change it to:
http://www.example.com/character/Jim_Carrey
Which of course, the ID is the row of the character name...
For that kind of example... I've tried to work with it, but don't seem to get most of the production of htaccess because I haven't worked with .htaccess a lot really.
It's actually the other way around:
On your website, you write all your links the way you want them to look. For instance http://www.site.com/character/Jim_Carrey
In your database, you add a field,
commonly called "slug" or
"post_slug" which refers to the
Jim_Carrey" part of the url. IT MUST
BE A UNIQUE ELEMENT, much in the way
of a primary key. So make sure you
have a function that does take care
of creating the slug based on a
given string (the post title for
example) and making sure there is no
duplicate.
Then in your .htaccess (on the root folder) you do
something like
RewriteEngine On
RewriteRule ^character/([a-z0-9_\-]+)/$ character.php?slug=$1 [L,NC]
Finally, in your character.php
script, you do a database query not
against the ID, but against the
post_slug field.
If you're only using the character's name, then something like the following would do
RewriteRule ^character/(.*)$ /character.php?slug=$1
with a URL of eg http://www.example.com/character/Jim_Carrey. You'll then need to look up the character's name in the database using the slug passed in, as you won't have the ID to look it up with.
Alternatively you could include the ID in the URL if you need it, vis:
RewriteRule ^character/([0-9]+)/.*$ /character.php?id=$1
This way you could have a URL like http://www.example.com/character/3/Jim_Carrey which would include the character name (for SEO reasons etc etc), but also the ID which you could then look up directly in your database.
Edit a small PHP example for you re the first one:
<?php
// ... database connection stuff here etc
$slug = $_GET["slug"];
// IMPORTANT: perform some data sanitization here
// I'm just going to make sure it's only letters, numbers and
// hyphens/underscores as an example.
$slug = preg_replace("/[^a-zA-Z0-9\-_]+/", "", $slug);
// Now look up in your database
// Ideally you'd have a slug column to compare this with, that you can fill
// when your record is created/updated
// You'd also be best off using bound parameters here, rather than directly
// adding the data into the query string. I personally use the MDB2 PEAR
// module but feel free to use whatever you normally use.
$qry = "SELECT * FROM characters WHERE slug='" . $slug . "'";
$result = mysql_query($qry, $db);
// do something based on this result, fail if none found
// or show details if OK etc
// ...
?>
Hope this helps! As always, use bound parameters where possible for your queries, and perform sanitization of your user data well. The PEAR MDB2 module has a nice page on how to do this here.
Edit 2 a quick and dirty setup :-)
.htaccess file as follows:
RewriteEngine On
RewriteRule ^character/(.*)$ /character.php?slug=$1
Your .htaccess file would ideally be in the root of your site. Eg /home/wayne/public_html/ or wherever your index file is served from
A URL to match that would be http://www.example.com/character/Jim_Carrey - with the phrase "Jim_Carrey" appearing in your $_GET array as $_GET["slug"]. NB apologies, wrote that PHP sleepy last night above so no wonder $_POST wouldn't work as its a GET request :-) I've updated it now!
Finally you need to make sure that your host supports the use of .htaccess files. The setup of this is out of the scope of SO so any Apache configuration questions you'd be best asking over at https://serverfault.com/
I'm fairly certain you can't do this through htaccess. You'll need to do it in PHP, querying the database using the information from the url (?id=3) and then calling the Header Function using what you've learned from the database.
Sounds like you might be able to use mod_rewrite rewritemap.