How to remove id from url in laravel - laravel

I am trying to remove id from the url. My urls's are something like this,
https://www.example.com/p/29/web-test
But, I want the url like
https://www.example.com/p/web-test
And my route is,
Route::get('/p/{id}/{any}','TestController#dynamic_page')->name('dynamic.page');
I have tried several codes .htacces like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ p/$1 [L]
But its not working, can anyone please help me out, I am poorly trapped in it,

Try this
Encrypt the $id in blade
{{ route('route_name', encrypt($id)) }}
And then decrypt id in crontroller.
$id = decrypt($id)
It will display encrypted id in URL.

You can't do what you are trying to achieve because if you remove the id from the url, using a .htaccess file or any other redirection method, the final route will still hit Laravel at the end.
If this route does not contain the required data (in your case, the id), it will simply not work.
The only way to "hide" the id from the url while still getting it on the server side (in Laravel) would be to change it after the page has been loaded, using javascript. But to be honnest it is ugly and I strongly discourage you of doing this.
The good news is, putting the id in the url is not a bad practice. Doing what has been proposed in the comments, https://www.example.com/p/web-test-29, is totally fine. Also, using a unique slug could also do the job (and this time, without the id).
It's all up to you, both these methods are technically OK and SEO friendly.
As a side note, since you mentioned it, I would advice against the use of .htaccess files whenever it is possible, since it'll bind your application to Apache and you might run into troubles if you want to switch to another web server later, like Nginx.

Related

Joomla trailing slash on categories

I would like to know how to add a trailing slash to joomla categories, but have no extension on articles.
Example, i want- xxx.com/category1/ and xxx.com/category1/article
Currently google sees- xxx.com/category1/ and xxx.com/category1 as duplicate content.
I would say you probably want to keep the URL without the trailing slash rather than the other way around. This just keeps it cleaner in the event that you have a category page which later needs a query string on the end of it. For example /categroy1?page=2 reads better than /categroy1/?page=2. This is personal preference though, Google does not really care so long as it's consistent.
The easy way to achieve what you are trying to do is through the .htaccess file in the root of your website.
Seems to be a few threads online about this subject. I've just tried a few of the solutions myself and the one that seems to work best is listed here https://forum.joomla.org/viewtopic.php?t=701030.
In summary you need to add the following to your .htaccess file just below the 'RewriteEngine On' statement.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !administrator
RewriteCond %{REQUEST_URI} !component
RewriteRule ^(.*)/$ $1 [R=301,L]
Note that the Rewrite Base is needed, but if you are running your site in a sub folder (as you might in dev) you need to include that folder in the RewriteBase.
Apparently the !administrator and !component statements are needed to prevent some infinite loops when viewing the admin area, or a component content page without a SEF URL. There may be other exclusions that are needed.
Other options to solve your problem include canonical tags on the pages to tell the search engines which one you want indexed. Extensions to perform the redirect for you. Or if you have a really small site, use Joomla!'s redirect component to redirect every page you don't want to the one you do.
Hope this helps.
KevBallard

.htaccess MOD_REWRITE specific cases

I am really struggling with .htaccess since I can't figure out how the syntax actually works and what the commands are. Let me describe what I need to achieve and maybe someone could guide me to the right answer:
I have a website which works with JQuery Ajax. So there is one single index.php file in the root folder. Any request of a sub-page via internal link(mysite.com/contact.php) is going through Ajax and the content information gets loaded from a folder called "/pages/" to the index.php.
But if a user enters the url itself (mysite.com/content.php) Everything breaks since the directory actually doesn't exist. Remember the content files are inside /pages/. And a direct access to the raw content files would not display the site, but only the information in raw form.
To solve this I started using .htaccess to pass not existing directory request to php, which passes it to Ajax. But here I got stuck. I am really bad with .htacces. This is what I got from the internet:
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This works for the most part, except if the request goes through a subfolder (mysite.com/download/version1)
So here is what I need .htaccess to do:
Don't care if it is a folder or a file request, pass it to php.
(optional) Do not allow direct access to /pages/ folder, but do allow if request comes via Ajax
Do not mess up request calls for resource files. (This is important. When I fiddled around, I got many errors since .htaccess was also interfearing in resource file (.css/.js/.png) calls)
Feel free to take a look at the website trough: GitHub
The php part is inside index.php. The Ajax is done in PageHandler.js

.htacces redirect for AJAX search engine indexing

Ok, so I made a pure html/javascript AJAX website, but I want my pages indexable by Google.
I have my content files with meta information in plain html, but without menubar etc. and I have my index.htm with all the menubars, javascript AJAX stuff, etc.
To make AJAX indexable for google, my URL's should look like "<something>#!<somthingelse>", which Google indexbot will change to "<something>?_escaped_fragment_=<somethingelse>", such that my server knows it should return the content directly, instead of the page that loads it via AJAX.
However, since my server is stupid, as it doesn't use server side processing, I need to perform a trick via htaccess (which is where I fail :( )
The idea is as following:
I have my fancy URL's http://mysite.com/page1#!1, http://mysite.com/page2#!1, etc.
Normally, htaccess should rewrite that to /index.htm?page=page1 such that my AJAX reads the URL param and automagically loads page1.htm content file.
For Google indexer, it should ignore this rewrite for any url containing "?_escaped_fragment_=1" such that the url points to the content page directly
This way I have to make a small compromise by putting #!1 in every fancy URL, but as far as I can think of, it is the only way to do this without server side processing (except for htaccess of course)
I just cant seem to get the rewrite rules to do this.
Here's what ive come up with so far:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|.*&)_escaped_fragment_=1(&.*|$)
RewriteRule ^(.*)$ %1 [L,R=301]
RewriteRule ^(.*)$ /index.htm?page=%1 [L,R=301]

Using mod_rewrite or htaccess to apply 301's to entire site structure, with URL's built using query strings

I am taking over an old website, and need to change the domain name.
The current domain is http://www.example.com/folder/ and the new domain is http://example.school.nz
An example of an existing and desired URL is:
Before: http://www.example.com/folder/index.php?page=sport
Desired: http://example.school.nz/about-us/sport
The site is built using CMSMS (http://www.cmsmadesimple.org), so pages and URL's are generated through the CMS, although it's easy enough to export a list of URL's.
Ideally I could set up 301 redirects for the entire site so the user doesn't get 404'd. Any clues to the easiest way to accomplish this?
It depends on how general you want things to be, but this set of rules will do what you asked in your question:
RewriteCond %{HTTP_HOST} .*
RewriteCond %{QUERY_STRING} page=sport
RewriteRule ^folder/index.php http://example.school.nz/about-us/sport? [L,R=301]

How to have an exception to URL routing in CodeIgniter

I have some legacy code that needs to be used in a new application. I would like to write the new application in CodeIgniter, but I still need to have some way of accessing the old code. what I would like to do is have an exception in the routing so that any url that has the format of example.com/old_stuff/* goes to an old_stuff folder, and acts as a regular, un-routed applications, while any other url such as example.com/new_stuff would route to a new_stuff controller, for example. So essentially what I want it to have URLs behave as they usually would in CodeIgniter, with the exception of any that start with one certain string.
What's the best way to accomplish this?
place codeigniter at your web root, and have your folder old_stuff in the web root also.
then use .htaccess with these rules (assuming you have mod_rewrite)
RewriteEngine on
RewriteCond $1 !^(index\.php|images|old_stuff|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
then, a uri beginning with old_stuff will just serve up the content bypassing codeigniter.

Resources