I am working on a webapp. There are items accessible the way using item id in the url web.site/123, where 123 is item id. I want to redirect visitors to the full url page like web.site/123-the-item, and I also want to use full links within my website.
Any good idea how to do it?
I don't need the code, I am looking for a simple yet nice and effective idea. The title is obviously in the database, so there will be one select needed anyhow. But I am not sure, if I should redirect once and every single time visitor visits the site without checking if the url is full (doesn't seem nice) or should I check the url and redirect only if it doesn't match the title?
I am using nginx, php, mysql and laravel 5.2 in case there is a workaround for this scenario I am not aware of.
Try adding route like
Route::get('/{id}',['uses' => 'ContentController#byId'],'where'=>['id'=>'\d*']);
And in that action you can check in database if id exists and redirect user to proper url
You can check use regular expressions to check the if the incoming request has just the id and have them mapped to particular routes. In the digit route you can redirect.
E.g.
your route file should be something like this :
Route::get('item/{id}', function ($id) {
//redirect to the full name route
})
->where('id', '[0-9]+');
Route::get('item', 'SomeController#somemethod');
Read more here.
Related
I want to give custom urls to members of my site to have their pages under my site url like www.mysite.com/{theirurls}.
In database in user table i have a field userurl that keeps the url i give them.
In route i have written the code
Route::get('/{fpage?}', 'AController#fofpage');
In controller in fofpage function i take the variable and look for the value in database. If it belongs to someone redirects to this. If it isnt goes back to / (root).
The problem is that all the other urls like login and others doesnt work.
How can i solve this problem?
Thanks in advance
Put this route at the end of the web routes file to make it work:
Route::get('/{fpage?}', 'AController#fofpage');
You'll also want to validate user URLs and not allow users to enter values like login, register etc.
I know two ways for sending an ID to another page:
as URI segment. exemple: http://mypage.com/index.php/ID
as an input field in a form using POST or GET methods.
Is there another way than these tow?
The purpose is to have a list of records. When the user clicks on one of them, s/he gets the full details of the selected record in a different page.
"Is there another way than these two?"
You can store it in $_SESSION too. If that what you are curious about.
But it's not the best way with your current problem.
Simply use GET (www.yourpage.com/records/THEID)
Well, you might consider this to fall under your first option of a URL segment, but "URL segment" usually means some part of the URL before any GET parameters...just to make sure you're aware of this, you can also put in GET parameters in a URL yourself without creating a form.
These days, URI segments like index.php/123 are popular, but traditionally the way to link to a detail page like that was index.php?id=123; then you can access the ID using $_GET['id'].
I have url, localhost/user/about/id/5, i want it to convert to a like localhost/john.doe/about, is it possible to do it in Yii?
john.doe refers to username
about refers to action
i want to hide controller name, in this case user
Thanks for the help
Yes you'll need to use a custom UrlRule as in the docs here (Using Custom Url Classes). You can then strip apart the URL in your class, try and find a user name, if it doesn't exists simply return false and let the rest of the URL rules process.
Bear in mind the higher up the order of URL rules you place your custom one, the more often it will be run (as UrlManager will exit on the first matching rule) so it has performance implications if you just put it right at the top.
Bonus
This will also help you in generating URLs as you can just pass a user name as a parameter to a normal URL and have your class do the complicated bit.
My site has articles and I would like the URL to be like so:
example.com/(article id)/(article title)
There are two reason I would like the URLs like so, SEO, and readability.
This setup is close to how stackoverflow is set up. And just like on stackoverflow, I don't want the title to be required but if it's not including in the I want the back-end to append the title to the URL.
To understand what I mean, take the current page's URL, remove the title form it and go to that new url. If you notice when the page is refreshed the title was automatically appended to the URL.
When I think about how to achieve this, it requires two queries. The first query will get the title based on the id, then form the new url, and redirect to it. The second URL will query the database with the id again this time displaying the article.
Is this the only way? Can I append the extra param without actually redirecting?
Side note: I am using cakePHP
I think that yes, this is the only way.
Call #1 to example.com/(article id): the server would query the database for the id, see that the url isn't the canonical one[1] and do a redirect.
Call #2 to example.com/(article id)/(article title) would behave as normal, meaning some more queries to the database...
^ I suggest you store an article's generated url path so you could use it in other places too.
This way, if no title was provided, the path would just be "/(article id)" and you wouldn't need to do a redirect here.
I'm looking to use mod_rewrite to mask user profiles on my site. However, their profiles are decided by their id in the format /profile.php?id=1. Both 'user' and 'id' are in the mySQL table 'users'. Is there a way to reroute the URL to read /user? Sorry if that's badly explained!
RewriteEngine on
RewriteRule ^profile/([^/\.]+)/?$ /profile.php?user=$1 [L]
This will cause all /profile/username to be redirected to profile.php?user=username. You can then check in your PHP:
if (isset($_GET['user'])) {
// Check if the username exists.
// SELECT ... FROM users WHERE username = ..
}
.htaccess will not be able to use the info from the database to do what you desire. Even if it could you shouldn't do it :).
But what you can do is to change the profile.php to be able to read an user by the name. Let's say to make profile.php able to process a new parameter name= and you add logic in it to look in the database either via an id or via the name. If you have this then is actually trivial to route / to /profile.php?name=.
You will also have to change the website generation to output proper urls.
Also look at Cristian's answer for more technical details. It's the same idea in the end.
You wouldnt change this in htaccess, or with any kind of config/rewrite rule. This would be done in your application PHP if I understand what you want.
in /profile.php?id=1 profile.php is (rewrite rules aside) the source PHP file, and id is a GET parameter. Its like an argument to profile.php when it executed. They come in the form source.php?get1=val1?get2=val2.... and you access them through the array $_GET
If you don't want users to see that id, you can do one of two things.
Replace it with user, as you suggest. URL would be 'profile.php?username=XXX'
Then in your code you will presumably need to perform a simple database query to get the id from the user name. Should be about 5 lines towards the top of the code in profile.php and the rest can remain unchanged Beware that this demands unique user names in case you werent before, and some special characters might be a problem with get parameters in the URL? For most user names, uniqueness is required and special characters are not allowed, so hopefully this isnt a problem.
Keep id but make it a POST peremeters. URL would be 'profile.php'
POST parameters are similar to get but they are not visible to the user because they aren part of the URL string. Just switch $_GET to $_POST in profile.php. The code will still identify users by id, but they won't be able to see their IDs.
In either situation you will need to change any incoming links.actions that lead to the descrubed URL that we are changing.
EDIT - Christian's has a good suggestion which is similar to #1 above but would allow for nicer looking URLs like profile/XXX rather than 'profile.php?username=XXX'. Code within profile.php would ne hte same, his changes are in profile.php are the same