Forum Moderators: phranque

Message Too Old, No Replies

Rewriting part of the url structure in htaccess

Rewriting URLs in htaccess

         

denverdonate

3:54 am on Apr 10, 2019 (gmt 0)

5+ Year Member



Part of my URL structure changed can I rewrite part of a URL in htaccess instead of updating thousands of urls one-by-one by hand?

mywebsite . com /old/component /tag/colorado-sunday-meetings

to

mywebsite . com /new/component /tag/colorado-sunday-meetings


If it was this one url I could 301 it but it's thousands of urls with that 1 change in the url structure.

I have been looking for examples online but I don't know if any of these work and where I would modify if so.

Removing the Query String
RewriteRule ^/url /url?

Adding to the Query String
Keep the existing query string using the Query String Append flag, but add var=val to the end.
RewriteRule ^/url /url?var=val [QSA]

Rewriting For Certain Query Strings
Rewrite URLs like http://askapache.com/url1?var=val to http://askapache.com/url2?var=val but don't rewrite if val isn't present.
RewriteCond %{QUERY_STRING} val
RewriteRule ^/url1 /url2

Modifying the Query String
Change any single instance of val in the query string to other_val when accessing /path. Note that %1 and %2 are back-references to the matched part of the regular expression in the previous RewriteCond.
RewriteCond %{QUERY_STRING} ^(.*)val(.*)$
RewriteRule /path /path?%1other_val%2

phranque

4:48 am on Apr 10, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], denverdonate!

can I rewrite part of a URL in htaccess ...

If it was this one url I could 301 it

just to clarify, a 301 is an external redirect
a rewrite is internal to the server.

mywebsite . com /old/component /tag/colorado-sunday-meetings

to

mywebsite . com /new/component /tag/colorado-sunday-meetings

you want to redirect /some/old/path/same/old-stuff to /some/new/path/same/old-stuff if i understand correctly.
are /some/old/path/ and /some/new/path/ variable or fixed?

Query String

you aren't doing anything relevant to a query string here.
the query string is the part of the url that follows the question mark.

lucy24

6:15 pm on Apr 10, 2019 (gmt 0)

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



RewriteRule ^/url
The subject line of this thread says explicitly “htaccess”. A leading slash / is used only when a RewriteRule is lying loose in config (most likely in a VirtualHost envelope). In any directory context, including htaccess, you omit the leading / slash.

Query strings are reappended by default, so you don’t need to think about that unless you are also changing that part of the URL, like from /directory/index.php?page=do-stuff-here to /directory/do-stuff-here.html. It sounds as if that isn’t part of your URL structure anyway.

not2easy

7:17 pm on Apr 10, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hi denverdonate and welcome!

It looks like you only want to change the folder structure in URLs from
example.com/old/component/tag/pagename
to
example.com/new/component/tag/pagename
and you are using your .htaccess file (rather than a .conf configuration file) to do that. Is this correct so far?

It helps when we're all on the same page, so I also need to ask - are these URLs generated by a CMS such as WordPress? If so, can I assume that the link structure has already been changed internally - within the settings? So you would want to 301 rewrite requests for
example.com/old/component/tag/pagename
to see
example.com/new/component/tag/pagename
instead. Is this correct?

In all cases does /component/ remain the same? Are we changing only /old/ to /new/ and capturing
/component/tag/pagename
from the old URL to add it back on after the /new/ change?

I think once we get a clearer picture, you can get some help.

Tip: If you use "example.com" instead of "mywebsite . com" it won't automatically link and be unreadable.

denverdonate

5:31 pm on Apr 11, 2019 (gmt 0)

5+ Year Member



Ok, sorry bout that here's a little more detail.
The CMS I use is Joomla and my hosting gives me cPanel access so if there's another way I'm fine with that!

OLD: ……..com/vehicle-donation-colorado/tag/aurora-aa-meetings

NEW: ……..com/component/tortags/tag/aurora-aa-meetings

I only need this: /vehicle-donation-colorado/ changed to /component/tortags/

That would fix all the thousands of links! I actually didn't know the 301 is external, that's awesome...my first post here and I'm already learning, nice.

not2easy

5:48 pm on Apr 11, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Another tip - since you mentioned using cPanel: be careful using cPanel for rewrites/redirects because it typically uses the Apache default which is a 302 status (temporary) which is not what is usually intended.

IF you need to use it, be sure to check your logs to see the server response to be sure you have a 301 (permanent) response.

lucy24

6:24 pm on Apr 11, 2019 (gmt 0)

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



my hosting gives me cPanel access
I should hope so. But better yet is to open the htaccess file itself in a text editor, edit, and upload the changed version. Save a copy locally as something like “htaccess_mysite” without leading . dot, and give it its correct name when uploading. (You can edit the htaccess on your server directly, but it isn't advisable unless it’s a very simple edit.)

I only need this:
Well, that sounds like a single conditionless RewriteRule. You could even do it in mod_alias (RedirectMatch) if you don’t mind the occasional double redirect.

You don’t say whether you are on 2.2 or 2.4, but the wording of the rule will be identical either way. What have you tried so far? If you have looked through earlier threads in this subforum, you know that What have you tried so far? is always the first question. It isn’t like That Other Forum where they write your code for you. (Or, more likely, they reply snippily that this question has previously been asked and answered.)