Rewrite url

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Rewrite url

Post by YoussefSiblini »

Hi,

I am using joomla, I want to get rid of the categories in my url, so I want:

Code: Select all

http://www.mygreatmove.com/before-you-arrive/neighbourhood
to be
http://www.mygreatmove.com/neighbourhood
I used this in the .htaccess file:

Code: Select all

RewriteEngine On
RewriteRule ^before-you-arrive(.*)$ http://mygreatmove.com/neighbourhood [R=301,L]
But this is redirecting to http://mygreatmove.com/neighbourhood and giving me a 404 error.

I want to know how to rewrite and not to redirect?

I will appreciate any help :)

Joe
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Rewrite url

Post by McInfo »

The "R=301" flag in your RewriteRule means that the browser should "Redirect", or make a new request, for the rewritten URL and the server should send a status code of 301 when telling the browser to do so.
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: Rewrite url

Post by YoussefSiblini »

Thank you for your reply,

This is my first time using the .htaccess file, what I did now is that I got rid of the [R=301,L]:

Code: Select all

RewriteRule ^before-you-arrive(.*)$ http://mygreatmove.com/neighbourhood
But it is not doing anything now.
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: Rewrite url

Post by YoussefSiblini »

My latest code I ended up with is:

Code: Select all

RewriteEngine On
RewriteBase /
RewriteRule ^/neighbourhood /before-you-arrive/neighbourhood [L]
and still not making any changes.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Rewrite url

Post by McInfo »

As the Apache documentation says, because the rewrite is defined in .htaccess context, "the path prefix of the directory containing the .htaccess file is stripped before matching." So try removing the first slash.

Code: Select all

RewriteRule ^neighbourhood /before-you-arrive/neighbourhood [L]
If that doesn't work (and I don't really expect it to), read the Joomla! documentation on search engine friendly URLs. Pay particular attention to where it mentions aliases. You may be able to define an alias so the rewrite is done by Joomla! instead of Apache Server.

I suspect the problem with doing an internal rewrite using the RewriteRule approach is that, while you achieve the user seeing a final REQUEST_URI of "/neighbourhood", Joomla! sees that same REQUEST_URI and not the "/before-you-arrive/neighbourhood" that it needs for its routing logic. When the RewriteRule redirects a user from "/neighbourhood", Joomla! receives the "/before-you-arrive/neighbourhood" REQUEST_URI, but the user sees the long version too.
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: Rewrite url

Post by YoussefSiblini »

Hi Guys thank you so much for your help,

I solved this issue, I found a helpful topic on line that helped me lots so I will copy and paste it here may be will be useful for some users who are having the same issue:

Code: Select all

(a) Create a new menu (NOT menu item)
(b) In the new menu created create a menu item for book1
(c) Edit the menu item book1 in the existing menu (e.g., Main Menu) and change the menu item to be Menu Alias and point that to the menu item created in step (b)
Post Reply