Forum Moderators: phranque

Message Too Old, No Replies

Stripping characters at end of URL

         

gardel

5:24 pm on Jun 7, 2012 (gmt 0)

10+ Year Member



I am very confused by htaccess.

I need a complete htaccess snippet that would takes a URL that ends in "-nnnn" (where "n" is a digit) and replaces "-nnnn" with a trailing slash.

For example, a url such as:
http://example.com/2011/01/18/say-it-aint-so-ronaldo-2497


is rewritten to:
http://example.com/2011/01/18/say-it-aint-so-ronaldo/


Thanks.

lucy24

6:39 pm on Jun 7, 2012 (gmt 0)

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



Awright, that does it. Time to assemble that boilerplate.

gardel: But what did I do? I just asked an innocent question!

me: Well, someone has to be first. You'll have the fun of sneering at my typos.

Why We Make You Do It Yourself

There are plenty of forums where you can post a "how-to" question and get a fairly immediate answer. The answer may even be correct. But WebmasterWorld is about teaching you how to do it yourself. That way you can roll your own htaccess-- not just for today's problem but for tomorrow's almost identical one.

Here is the analogy:

Your child's room needs cleaning. You know that you can clean it yourself much faster and better than if you have to stand over your child and force him to do it right. But if you do the "make him get it right" part often enough, you will have raised a child who knows how to clean his room-- and who will some day stand glowering over his own children in the same circumstances.

Some day, someone else will post a question in the WebmasterWorld forums and you'll say "Hey, I know the answer to that one!"

Besides, when someone with a very low post count asks a question, that person may have posted the identical question in six different forums. They grab the first answer they get, and never even come back to the others. You can understand that this is infuriating for the person who took the time to compose an answer.


Now the question: is one or the other of your URLs-- either the -2497/ version or the / version-- a "real" directory or a "real" page? Or is this the prelude to some behind-the-scenes rewriting?

gardel

7:06 pm on Jun 7, 2012 (gmt 0)

10+ Year Member



Thanks, and your sneering at me is very valid. There's probably no such thing as an innocent question.

The site is served by Wordpress. At some point, the working URLs would have the "-nnnn" appended to the end. The site was redone using a different Wordpress framework, and it no longer appends "-nnnn" to the URL. This means that older external links to the specific Wordpress posts no longer work. Stripping the "-nnnn from the no-longer-working link does open the page.

g1smd

7:55 pm on Jun 7, 2012 (gmt 0)

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



For this you would think that you need a RewriteRule configured to deliver a 301 redirect.

The RegEx pattern needs to capture all except the final hyphen and number. Since patterns are more efficient when evaluated "from the left", this is not at all easy to do.

Instead a different method is more useful. Set up a RewriteRule configured as a rewrite where the RegEx pattern matches "(something hyphen) one or multiple times, the previous then followed by digits".

RewriteRule ^([^-]+-)+[0-9]+$ /special.php [L]

Rewrite these requests to a simple PHP script that slices up the requested URL using "explode" or similar, builds the new URL and then uses the HEADER directive to send the 301 redirect response.

lucy24

10:23 pm on Jun 7, 2012 (gmt 0)

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



If the examples given in the OP are typical*, the whole URL can be laid out pretty unambiguously:

http://example.com/2011/01/18/say-it-aint-so-ronaldo-2497

http://example.com/2011/01/18/say-it-aint-so-ronaldo/

^(\d\d\d\d/\d\d/\d\d/[-a-z]+[a-z])-\d+

$1/

requiring only a single RegEx hiccup: at the hyphen following "ronaldo". Matter of fact I'm not sure the last [a-z] is necessary, since the result would be the same.

If the title part can contain numerals immediately after a hyphen, then yes indeed it's php time.


* Raymond? You out there? Gosh, this URL format looks familiar ;)

gardel

6:03 pm on Jun 8, 2012 (gmt 0)

10+ Year Member



Thanks Lucy. I've been trying to work your example into .htaccess, but have been unable to make it work. For example, I entered the following lines at the bottom:

RewriteEngine on
RewriteRule ^(\d\d\d\d/\d\d/\d\d/[-a-z]+[a-z])-\d+ $1/ [R=301,L]

This did not work (but it did not break the site, either). How would you edit it?

gardel

7:56 pm on Jun 8, 2012 (gmt 0)

10+ Year Member



I got it working. In case someone in the future bumps into this thread, the following two lines near the top of .htaccess solved it for me:

RewriteCond %{REQUEST_URI} ^(.*)-[0-9]{4}$
RewriteRule ^(.*)-[0-9]{4}$ /$1 [R=301,L]

g1smd

8:11 pm on Jun 8, 2012 (gmt 0)

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



Don't use (.*) at the beginning or in the middle of a RegEx pattern.

The rule target should include the protocol and domain.

The RewriteCond is completely redundant as it merely repeats the RegEx pattern found in the rule.

You completely overlooked the solution I provided in an earlier post.