How to replace post ID with url slug - javascript

I have a DB table that saves my title as a slug (ie: this-is-my-title-slug) but I have no clue how to use it in the url.
Example ofor current url http://www.example.com/post.php?id=102
What I want is
http://www.example.com/this-is-a-slug.

You're looking at SEO urls. And a slug is something you would use as a name of an article on www.example.com/articles/article-title-slug for instance, what you probably mean is a permalinks which basically are SEO urls.
If you're running an apache server with PHP and you have a vhost setup then you can use that vhost to allow url rewriting (Apache mod_rewrite).
This allows you to place a file called .htaccess in the webroot of your project (e.g. same directory as root index.php file), within this file you can set up rules that allow such rewriting. What often happens is that you rewrite everything to the root index.php file and you capture the URI and parse the parameters by splitting it on the forward slash (/) character. This is basically a router idea.
The advantage of parsing the URI yourself is that you don't really have to do much in the .htaccess file which is much more complex to handle than building a router in PHP itself as well.
A good example of a .htaccess file can be found on this SO answer
about how to get going with that. There are also plenty of other tutorials to get started with this other than that answer.
DigitalOcean - How to use the .htaccess file
TutsPlus - The ultimate guide to .htaccess
It's been a while since I've done it myself but if you have questions, just ask them and I'll see if I can help but anyways, this should point you in the right direction.

Related

Do html redirect based on header request

I have near zero html and/or javascript experience and I'm finding so little information on this topic that I suspect I've got a fundamental misunderstanding on what I'm trying to do. But hey, software developers are somehow fungible so this ended up on my plate.
I've got a git project that includes several files. It looks like this:
git repo:
index.html
my_data.json
my_data.html
.github/workflows
some_workflow.yaml
some_folder
some_other_file.json
I want to publish this on github-pages, which expects the page to be at index.html. When a user does a request to the github-pages url the files that get returned should be based off the request header that the user uses.
For example:
Navigating to the webpage http://my-org.github.io/my-project in a
web browser would end up at the index.html file and should be
redirected to my_data.html.
curl -H "Accept: application/json" "http://my-org.github.io/my-project" should return the my_data.json
file.
A real nice bonus would be if I could navigate to http://my-org.github.io/my-project.json and get the json file
Redirecting to the my_data.html file by setting up a meta <http-equiv="refresh" content="0; url='./my_data.html'"> seems pretty standard but I can't figure out how to do the json part. Most tutorials specify what content negototiation is but then get all hand-wavey at the implementation or refer to API frameworks that have it all baked in.
Is there some kind of javascript if statement I could use to handle the TWO mime-types I expect to handle?

Add language path into browser address bar

I'm looking for information about adding extra information into browser address bar. for example language path.
So what type of code should I look for if I wanna change browser address from mysite.com/index.php to mysite.com/EN/index.php
but at the same time, I don't have to make an extra folder for each language file what I add for the website.
It depends on which web server is hosting the application. If apache, a way to get this is using AliasMatch directive. See https://httpd.apache.org/docs/2.4/mod/mod_alias.html#aliasmatch. This applies to httpd.conf (global apache configuration file) or .htaccess (local apache configuration file) and requires mod_alias.
Example:
AliasMatch "^/(EN|FR|PT)/(.*)" "/local/path/$2"
will accept /EN/... /FR/... and /PT/... .
or
AliasMatch "^/([A-Z]{2})/(.*)" "/local/path/$2"
will accept any two upper case letters as prefix.
After getting this working, in order to determine which language to show, you should check $_SERVER[‘REQUEST_URI’] variable in your PHP script.
Just go to the public folder in the hosting and create a new folder in it named EN and copy index.php to the folder EN and try the path mysite.com/EN/index.php .It should work.
You will probably need to handle this on the server side. When a request is made to mysite.com/EN/index.php, the server checks the request URL for the language part EN and serves a webpage with the corresponding language from wherever it resides in the file structure.

I want to upload a file by ajax but I don't want the server direct link appears

Everyone know when upload a file by ajax we must add the direct link to the php file which will upload the file, ex: url: site.com/upload.php.
What I want is that this link site.com/upload.php will not be the direct link to upload the file, but it will be just redirection to the up.php file which will be upload the file actually.
The aim is I want to disappear the link which responsible for upload the client file.
Is this idea can be implemented ?
IMHO If your target with that is security, You're targeting the wrong phase.
Instead of protecting the PHP File location, you can protect the file itself.
the UP.PHP can have some "filename" protection using mod rewrite (How to remove .php or .html extension from single page?)
The UP.PHP can have authentication and authorization through your site auth/autho system, and if you want it public, do it keeping FORM ID pair or something.
The address of the UP.PHP will look something like: site.com/upload/receiver and will point to your file, if you want to protect even this address, you should use anything else but JS Ajax, like flash, java, or anything else.
Just follow any how to do it step-by-step.
https://www.google.com.br/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#safe=off&q=flash+file+uploader+example
Use SSL encryption...

Sails.js regex routes

I am building a simple sails.js project and implementing the front end with backbone.
Ideally I want a single route to the one index page in which my backbone app is served.
'/*': {
view: 'home/index'
}
This is great, so any URL now goes to the homepage. Except now, all the routes to any assets (.js, .css, .html, .jpg) do not work anymore.
I can see this comment in the config.routes.js:
// NOTE:
// You'll still want to allow requests through to the static assets,
// so we need to set up this route to ignore URLs that have a trailing ".":
// (e.g. your javascript, CSS, and image files)
'get /*(^.*)': 'UserController.profile'
But it doesn't make any sense to me. How do I ignore routes with a file extensions.
I have also prefixed all my CRUD url's with 'api', localhost:1337/api/controller/ so a regex route to exclude forwarding those would also be required. I cannot find any information on how to do this anywhere.
I must be missing something fundamental here.
Thanks!
You can use the skipAssets and skipRegex flags. This will not step on your static assets and will not block your api urls starting with /api. I use this with sails.js and angular in html5Mode.
"get *":{
controller:"PagesController",
action:"index",
skipAssets: true,
skipRegex: /^\/api\/.*$/
}
I think this is a bug but I found a workaround solution.
'get /[^.?]+?': 'UserController.profile'
or you can use other one. only /user and /profile will exec UserController.profile. static directory still work great.
'get /:action( user | profile)': 'UserController.profile'

Java Script to get localhost+htdocs folder

I have following url to be build,
http://localhost/myweb/cart/index.php
I want to get the http://localhost/myweb/ bit build dynamically.
To do that on my live web site which is http://www.myweb.com/cart/index.php I can use the following JavaScript code,
var http = location.protocol;
var slashes = http.concat("//");
var host = slashes.concat(window.location.hostname);
But how do I get my development environment to work since it has http://localhost/myweb/? If I run the above code it will give me http://localhost/ only.
Any suggestions?
window.location.pathname is the thing you search for.
I would suggest you to read the MDN description of window.location. Like everything else in MDN, this is also really straightforward and informative.
If you know that the URL has an unnecessary index.html part at the end you can:
var path = window.location.pathname.split('/');
path.pop();
path.join('/');
or you can slice it (since it is generally faster):
path.slice(0,path.lastIndexOf('/')+1)
EDIT:
Seeing your new question I can say that what you want can't be done consistently and safely by only the current URL.
You need the http://localhost/myweb/ part, which is the URL root of your application. In javascript you are able to get the protocol and domain of the url. On your live site these 2 match, but if your application resides in a subfolder (like the myweb folder at your localhost), this will fail.
What you need is to somehow identify the application URL (the URL root of your application).
The problem is that by only examining the URL, javascript cannot tell where your application resides.
Let's say you deploy your site to: http://localhost/myweb/site1/
You will have the following URL: http://localhost/myweb/site1/cart/index.php
Javascript can split your URL by the slashes (/) but it has no way of nowing how many subfolders it should select. For example from the URL above your application root can be any of the following: http://localhost/, http://localhost/myweb/, http://localhost/myweb/site1/, http://localhost/myweb/site1/cart/.
By an other approach (which I suggested first) you can drop the end of the URL (in your case the cart/index.php part). This will only work if your URL structure IS very rigid, so all the pages this script is executed on reside in one subfolder.
So it will break on the following URL: http://localhost/myweb/site1/gallery/old/index.php or similar.
Your best bet would be to make this a "config variable" in a separate file which you edit on every location it is deployed to.
Either as a PHP variable ($appRoot = "http://localhost/myweb/") which you generate the javascript with.
Or more simply a javascript variable (var appRoot = 'http://localhost/myweb/'). Make a separate js file, call it something like config.js, add the above line to it and reference it before your other javascripts.

Categories