Forum Moderators: phranque

Message Too Old, No Replies

change part of a URL

         

surrealillusions

6:19 pm on Nov 12, 2014 (gmt 0)

10+ Year Member



Hi,

I'm after a way of changing part of a URL.

example.com/change-this-part/some-other-page

so it reads

example.com/this-part/some-other-page

But, there are other pages after this URL, so, it could be

example.com/this-part/another-page
or
example.com/this-part/ooh-look-a-tree

I've tried a couple of ways I found online, but it resulted in a 500 error, such as this way:

RewriteRule ^(.+)/catid/ /$1 [R=301,L,NC]

Rewrite engine is on, and the rule was placed after it.

Thanks.

not2easy

8:13 pm on Nov 12, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



If I understand what you want to do, it is to make these pages that are at example.com/uglyname/page-name.html to appear to be found at example.com/nicename/page-name.html? If that is what you want to do, you'll need to rethink it. You can create a new folder called /nicename/ and move the pages there, then you can redirect to the URLs you want.

The problem is that if a request for http://www.example.com/uglyname/page-name.html is given to the server and a rewrite changes the name of the folder, it will be requesting those pages at http://www.example.com/nicename/page-name.html, but they aren't there. The server can't hand over pages it can't find where it was told to find them.

surrealillusions

8:24 pm on Nov 12, 2014 (gmt 0)

10+ Year Member



Site is made in Wordpress, so not able to move folders. It's actually a plugin that makes sub pages. I could find where to edit the plugin that generates the URL's, but, figured it'd be easier to just change the URL via the .htaccess to keep the plugin intact for future updates.

not2easy

8:34 pm on Nov 12, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Most WP related alterations need to be done within WP because the URL generation uses information based on the WP settings and stored in the database. It can be difficult to rewrite outside of the database and make it work. On the other hand there are plugins that can handle a lot of things. If a plugin is generating the URLs, you may be able to change the settings for the plugin.

lucy24

9:24 pm on Nov 12, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't see anything in the rule-as-quoted that would lead to a 500-class error, so I suspect a typo. But there are other issues.

For starters, the .+ business is wildly inefficient. Let the body of the rule say instead

RewriteRule ^([^/]+)/catid http://www.example.com/$1 [R=301,L]


If the part you're keeping contains more than one / slash, adjust as needed.

Now, if this is happening via WordPress or any other CMS, this external redirect needs to be accompanied by an internal rewrite that says something like (again mutatis mutandis)

RewriteRule ^([^/]+)/$ /$1/catid [L]


This part has to go before the existing WP part of your htaccess.

And now you need to go back and add a Condition to the first rule-- the one creating the external redirect-- so it says

RewriteCond %{THE_REQUEST} !catid
RewriteRule ^([^/]+)/catid http://www.example.com/$1 [R=301,L]

meaning "the business about /catid/ was NOT present in the original request that came in from outside".

Unless you've got a problem with wrongly cased links coming in from outside, leave off the [NC]. It just creates more work for the server.

As a general principle: Anything that you feel comfortable doing yourself in htaccess should be done there. Leaving it to the CMS creates much more work for the server.

surrealillusions

5:50 pm on Nov 13, 2014 (gmt 0)

10+ Year Member



Thanks for the replies.

With this:

RewriteCond %{THE_REQUEST} !catid
RewriteRule ^([^/]+)/catid http://www.example.com/$1 [R=301,L]

I've implemented it into the .htaccess, replacing the relevant URL's, but it's not working.

No 500 error, but the redirect/renaming isn't working. It just displays the original.

Those 2 lines go above/before the WP lines correct?

surrealillusions

8:37 pm on Nov 14, 2014 (gmt 0)

10+ Year Member



Found this, and somehow worked but with an error/infinite loop, but cant replicate it and only get the infinite loop or 500 error.

RedirectMatch 301 /otw-portfolio/(.*) /portfolio/$1

This alone doesn't work and causes an infinite loop. Adding in [L] after or [R=301, L] causes a 500 error. Why? And how do I prevent an infinite loop?

lucy24

3:05 am on Nov 15, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Flags such as [L] are not used with mod_alias, so that's your 500-class error. In addition, mod_alias can't consider the query string, and can't use Conditions. Finally, mod_alias works by reappending new material onto an existing path.

But you can ignore the entire preceding paragraph, because the short version is:

Do not use mod_alias (Redirect by that name) in any htaccess file that also contains RewriteRules. The server won't explode; you'll just get rules executing in the wrong order.

If you translate this
RedirectMatch 301 /otw-portfolio/(.*) /portfolio/$1 

into mod_rewrite syntax, you get
RewriteRule otw-portfolio/(.*) /portfolio/$1 [R=301,L]

which should really be-- in any module--
RedirectMatch 301 /otw-portfolio/(.*) http://www.example.com/portfolio/$1 

and
RewriteRule otw-portfolio/(.*) http://www.example.com/portfolio/$1 [R=301,L]


This, by itself, shouldn't create an infinite loop. The pattern looks for the element "otw-portfolio"; the target removes this element. No loop.

But you should use an anchor. Assuming the full URL in its original form was
www.example.com/otw-portfolio/more-stuff-here
a RewriteRule located in htaccess will normally say
RewriteRule ^otw-portfolio/more-stuff-here et cetera

It's probably easier if you cite the actual file and directory names in your questions. It is perfectly OK to do this, so long as the domain itself is called example.com.