Jump to content

Remove part of URl with .htaccess


Yannick Albert
 Share

Recommended Posts

Ahoi, pros!

Currently I've to create some frontend related user profiles. However I stuck at the routing part. All users are a child of a `users`-page and their url's look like this:

http://localhost/projects/ca/users/foo/

...I've to accomplish a url-structure like the following, so I tried redirect all these request to this:

http://localhost/projects/ca/foo/

Using .htaccess doesn't work for me in this case:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /projects/ca/
    RewriteRule ^users/(.*)$ $1
</IfModule>

Any ideas how to get this to work, maybe without .htaccess?

Link to comment
Share on other sites

  Currently I've to create some frontend related user profiles. However I stuck at the routing part. All users are a child of a `users`-page and their url's look like this:

http://localhost/projects/ca/users/foo/

...

Any ideas how to get this to work, maybe without .htaccess?

Hi yckart, welcome to the forums.

One approach could be using urlSegments:

Depending on what template/page you are using under /ca/, you may enable "Allow URL-Segments" under Templates -> YOURcaTemplate -> URLs.

- with your users pages you define urls like http://localhost/projects/ca/foo/ instead of http://localhost/projects/ca/users/foo/

- on the /ca/ page you first do a check if there is a URL-Segment ($input->urlSegment1) populated and switch to processing with /users/foo/

- if there is no $input->urlSegment1 process the request like you do it yet with /ca/

Link to comment
Share on other sites

Quote

Hi yckart, welcome to the forums.

 

One approach could be using urlSegments:

 

Depending on what template/page you are using under /ca/, you may enable "Allow URL-Segments" under Templates -> YOURcaTemplate -> URLs.

 

- with your users pages you define urls like http://localhost/projects/ca/foo/ instead of http://localhost/pro...s/ca/users/foo/

 

- on the /ca/ page you first do a check if there is a URL-Segment ($input->urlSegment1) populated and switch to processing with /users/foo/

 

- if there is no $input->urlSegment1 process the request like you do it yet with /ca/

Hi horst, thanks for the salutation and fast answer!

At the first, I've to say that my php-skills are really restricted. It would be great if you could share a simple snippet, or further instructions how to switch to processing with /users/foo/ and how to process the request like you do it yet with /ca/.

Just as a sidenote, I forgot to say that `http://localhost/projects/ca/` is where the pw-installation is located at and uses currently the default home-template.

Thanks for your help in advance :)

Link to comment
Share on other sites

Hi yckart,
 
I guess you are using the basic siteprofile that comes with PW per default. Following is the code of the home-template file. It is located under /site/templates/ and is named home.php. I have added a check for the url segment and the code you need to output a user page, whereas the original code of the home-template completly goes into the else-part:

<?php

/**
 * Home template
 *
 */

// first lets check if we have a urlSegment1
if( ! empty($input->urlSegment1) ) {

	// we have a url segment and want to get the page for it
	// we use ->get() for this, because get returns a single page, whereas ->find() returns an array of pages
	// but as we use the unique name of the users pages, there only can exist one page with that name
	$userpage = $pages->get("/users/{$input->urlSegment1}/");
	
	// lets check if we have got a valid page
	// if PW hasn't found a page, it returns a nullPage and that has always a id 0
	if($userpage->id == 0) {
		// we have no valid page, lets throw a 404 exception
		throw new Wire404Exception();
	}
	else {
		// we have a valid page, so lets output the complete users page
		echo $userpage->render();
	}
}
else {

	// if there is no url segment, output the homepage

	include("./head.inc");

	echo $page->body;

	include("./foot.inc");

}

 
This template will work if you use links like "/userpagename/" userpagename is the content of the namefield of your user page.
 
If you have a page /users/foo-bar/, you can call it /foo-bar/ and it will be rendered, if you use /fooBaz/ you will be redirected to a 404 page, and if you call / you go to the homepage. But you also can access the user pages with there native urls: /users/foo-bar/.
 
 
Hope that helps.

Edited by horst
  • Like 2
Link to comment
Share on other sites

Ok, good starting point, thanks so far! However, I think (also because of duplicate content) this approach is not the best. After a bit of research I found the perfect solution.

Using a simple hook does the trick:

http://processwire.com/talk/topic/1799-routes-and-rewriting-urls/?p=16708

wire()->addHookAfter('Page::path', null, function(HookEvent $event) {
  $page = $event->object;
  if ($page->template == 'account-profile') {
    $event->return = '/'.$page->name.'/';
  }
});

Anyway, thanks for the fast support and explanation!

Cheers,

Yannick

  • Like 1
Link to comment
Share on other sites

Actually you need both of these, urlSegments to render those pages on a different level, and the hook is just to conveniently modify the path (esier than manually createing them).

I don't see where duplicated content is a problem. EIther use canonical urls, or take care of it with a redirect or some hooks. :) But if you never have links to those (real locations) you won't have them visible anyway.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...