URL rewriting using NodeJS - node.js

I want to customize my URL rewriting but it seems not working as I'd like it to.
My old code :
var rewrite = require('express-urlrewrite');
exports.rewrite = function(app){
app.use(rewrite('/p/:id/:seoUrl', '/page/show/$1/$2'));
}
In my browser : http://mysite/p/1/seo-title (this URL works)
My new code :
var rewrite = require('express-urlrewrite');
exports.rewrite = function(app){
app.use(rewrite('/:seoUrl', '/page/show/$1/$2'));
}
In my browser : http://mysite/seo-title (ID not found)
Using NodeJS, is there a way to exclude the ID in the URL?

well. if you take a close look at this: '/page/show/$1/$2' you might notice that it accepts two arguments.
since you removed :id you'd have to replace $1 with a static id or remove it completely aswell as you have to change $2 to $1
so in the end your code might look like this: app.use(rewrite('/:seoUrl', '/page/show/1/$1'));
or like this: app.use(rewrite('/:seoUrl', '/page/show/$1'));

Related

append a value to existing url in reactjs

I have the following url and a parameter :
number:200
"http://localhost:8000/textpath"
I want the path to be like this:
"http://localhost:8000/textpath/200"
How do I do that using Reactjs?I want to use the appended url in fetch method as follows:
fetch("http://localhost:8000/textpath/200")
A simple append did the work.Doesn't have to complicate
fetch("http://localhost:8000/textpath/"+number)
Try using Template literals.
const url = `http://localhost:8000/textpath/${number}`
fetch(url);
where number = 200
Refer here for more information

How to rewriting url to split the last path by "dot" and redirect to a query string by splitted items?

I have a whois form that works with get query string and make results.
I want to redirect url from:
mysite.com/domain/google.com
to
mysite.com/domain?sld=google&tld=com
Thanks
In .htacees
For example try this --
Redirect /domain/google.com /domain?sld=google&tld=com
OR
Try this using through jquery for using any url
var baseurl = "mysite.com/domain/";
var a = window.location.pathname.split("/").pop();
var name = a.split(".")[0];
var name1 = a.split(".")[1];
alert(name);
alert(name1);
alert(baseurl+'?sld='+name+'&tld='+name1);
window.open(baseurl+'?sld='+name+'&tld='+name1);
Hope it will work for you

pretty url search string instead of question marks

I have been looking at other websites and see when you search, primary something like a search term, location and category you will see a pretty url like:
example.com/black-boots/new-york/shoes
instead of what I have now which is something like:
example.com/search-results/search?=black+boots&city=new+york&category=shoes
In my route I could start with something like:
router.get('/search-results/:search/:city/:category', shopController.getSearchResults);
And in the controller I could use req.params.city and so on to get the values from the url but the part that I can't figure out is a good way to get the text input values into the url using a get request.
Using GET by default gives me the 'ugly' looking url.
Basically the part that needs to go into the form
<form method="GET" action="/search-results/search/city/category">
Comments, plus this code sample for a GET request:
const form = document.getElementById('searchform');
form.addEventListener('submit', evt => {
const who = encodeURIComponent(document.getElementById('who').value);
const where = encodeURIComponent(document.getElementById('where').value);
const what = encodeURIComponent(document.getElementById('what').value);
window.location.href = `/${who}/${where}/${what}`;
}

Url rewrite using htaccess change query string in base url

I'm trying to remove string from the end of URL:
For example i have a structure like this:
http://sitename.com/category/first-category-name.php/?post_type=question
http://sitename.com/category/second-category-name.php/?post_type=question
http://sitename.com/category/...-category-name.php/?post_type=question
I would like to convert url from http://sitename.com/category/-----category-name/?post_type=question to http://sitename.com/category/-----category-name/post_type/question from the end of URL.
You can conditionally redirect if the post_type value exists in the query string.
// Get the category. This is here for example purposes.
$categoryName = 'first-category-name';
if (!empty($_GET['post_type'])) {
$postType = $_GET['post_type'];
header("Location: http://sitename.com/category/$categoryName/post_type/$postType", true, 301);
}
For security, you probably want to make sure $category and $postType are sanitized.

Codeigniter url rewrite issue when using pagination

I am learning codeigniter these days. I have done a website for my own use a couple of days ago where i have just applied pagination today but it seems there is problem with my url rewriting.
Here is my action inside my controller which grabs list of authors in a category
`public function index() {
$data['main_content'] = "templates/public/audio/author_view";
$data['authors'] = $this->author_model->get_authors();
$this->load->view("templates/public/template",$data);
}`
Here is my function that grabs value from db located inside my author model
`public function get_authors($type = 0) {
$this->db->select("author.name,author.image,author.slug");
$this->db->from("author");
$this->db->join("album","author.id = album.author_id");
$this->db->where("album.mime_type",$type);
$this->db->group_by("author.name");
$query = $this->db->get();
return $query->result();
}`
When i clicked on one of author grabbed it open all albums of selected author. then url looks link www.xyz.com/audio/album-nameHere is my code for this route.
$route['audio/(:any)'] = "audio/view_author_album";
At this stage it works fine. But now today when i applied pagination i found that this route will not do more work for me. I have added pagination in my index action Below you can see my code
public function index() {
$config['base_url'] = "http://localhost/mywebsite/audio/index/";
$config['total_rows'] = $this->author_model->get_total_rows();
$config['per_page'] = 1;
$this->pagination->initialize($config);
$data['main_content'] = "templates/public/audio/author_view";
$data['authors'] = $this->author_model->get_authors($config['per_page'], $this->uri->segment(3));
$this->load->view("templates/public/template",$data);
}
This open the details http://localhost/mysite/audio/index/2
against this url my route rule $route['audio/(:any)/(:any)'] = "audio/view_album_details"; works. It should grab the next page instead of my detail view. And url should be something like http://localhost/mysite/audio/2
I also tried $route['audio/(:num)'] = "audio/;
I will highly appropriate if anyone can help me in solving this problem.
i encounter the same issue and i solve it by using two same router
$route['a-b-c/([a-z0-9-]+)/([0-9]*)'] = "product/category/$1";
$route['a-b-c/([a-z0-9-]+)'] = "product/category/";

Resources