HTACCESS - how do you point to another URL with /?

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

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

HTACCESS - how do you point to another URL with /?

Post by simonmlewis »

We have many URLs that are dead, since they have "/mobile" at the start.
I know how to point one URL to another. But how do I point one URL that "starts" with '/mobile' to another?

ie

RewriteRule ^mobile/...../?$ /blog [QSA]

??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTACCESS - how do you point to another URL with /?

Post by Celauran »

Just capture the rest of the URL and do whatever you need to with it. This will strip out the mobile/ and leave the rest, for example. mobile/path/to/page would become path/to/page.

Code: Select all

RewriteRule ^mobile/(.*) $1 [L,QSA]
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS - how do you point to another URL with /?

Post by simonmlewis »

So if the url is:
[text]http://www.simon.co.uk/mobile/product/5 ... 5X51-SUPER[/text]

Code: Select all

RewriteRule ^mobile/(.*) /home [L,QSA]
Then it would point it to whatever URL I want, such as homepage, as it sees the 'mobile' in there?

RewriteRule ^mobile/(.*) $1 [L,QSA]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTACCESS - how do you point to another URL with /?

Post by Celauran »

Code: Select all

RewriteRule ^mobile/(.*) /home [L,QSA]
This would ignore everything after mobile and redirect to /home
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS - how do you point to another URL with /?

Post by simonmlewis »

How can the same be done if it starts like this:

[text]http://www.test.co.uk/ip_mobilehome.php?url=http://............[/text]

How would I do a 301 for all ip_mobilehome ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTACCESS - how do you point to another URL with /?

Post by Celauran »

What's the url= bit? Do you even need to do that via .htaccess?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS - how do you point to another URL with /?

Post by simonmlewis »

I don't know. It use to be used to take someone to ip_mobilehome.php, and then query their device before taking them to the relevant web page thru a PHP file.
I just wondered if I could do a 301 via HTACCESS for it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS - how do you point to another URL with /?

Post by simonmlewis »

Hi.
We still have this problem. with URLs like the one I mentioned.
I want to be able to point each one to the relevant product or category page.
So how do you do something like this:

Code: Select all

RewriteRule ^ip_mobilehome.php?url=http://www.site.co.uk/services?$  /services [QSA]
As that doesn't work.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTACCESS - how do you point to another URL with /?

Post by Celauran »

Something like this, maybe?

Code: Select all

RewriteCond %{REQUEST_URI} ip_mobilehome.php
RewriteCond %{QUERY_STRING} ^url=http://www.site.co.uk/(.*)&?
RewriteRule (.*) %1? [L]
Actual examples of inputs and their intended destinations would make this easier.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS - how do you point to another URL with /?

Post by simonmlewis »

If you'll igore the site.co.uk, that is an actual line I need to replace.
I have about 150 of them, so do I need to do those three lines you've shown, for each line? Or is there a shortened version, for each of the 150 ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTACCESS - how do you point to another URL with /?

Post by Celauran »

What this rule set is doing is checking if the page requested is ip_mobilehome.php, if the query string contains url=whatever, stripping the domain name, capturing the rest, and redirecting there.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS - how do you point to another URL with /?

Post by simonmlewis »

So what if "the rest" is inaccurate too, and we need to take them to a more appropriate URL?
So sometimes we might take them to /services, but otherwise it might be an old category URL that we need to take them to a new one.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTACCESS - how do you point to another URL with /?

Post by Celauran »

Then .htaccess probably isn't the right tool for the job.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTACCESS - how do you point to another URL with /?

Post by simonmlewis »

So if I want to point that long URL to another completely different internal URL, ie... /services should go to /newservices, I cannot do that in HTACCESS?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTACCESS - how do you point to another URL with /?

Post by Celauran »

Of course you can. Whether or not you should depends on how many cases you're dealing with and how complex the logic is going to be. If you're mostly redirecting to the url= URL with a few exceptions, sure. You just don't necessarily want to end up with hundreds of rewrite rules.
Post Reply