Log in

View Full Version : Resolved Regular Expression and Trailing Slashes



SaishuHane
04-10-2012, 03:38 PM
What I'm trying to do is make a regex that will get rid of the trailing slashes on my URL. I'm also using a Wordpress plugin for redirection that allows me to put / without doing \/ . The following is what's giving me the most trouble:

/tag/logo/
and
/tag/logo/feed/

I can get either the first or the second to work, but never both at the same time. The plugin has a input and output section, but I'm not quite sure how to work it out. If I get the first to work, the URL ends up looking like this on feed pages:

/tag/logofeed

So far, this is the last regex I've used:

Input: /category/((.*)(/feed)?)/
Output: /category/$1

I don't have that much experience writing these, but I was hoping to get the regex to work for both /logo/ and /logo/feed/ . Is what I'm trying to do even possible?

james438
04-10-2012, 04:50 PM
What do you mean both at the same time? Are you saying that you are trying to get an expression that will work with "/logo/" and then can be used with "/logo/feed/" or are you saying that you want a pattern to match "/logo/ and /logo/feed/".

If the former you can just do this:


<?php
$test="/logo/feed/";
$test = substr($test, 0, -1);
echo "$test";
?>

If the latter try:


<?php
$test="/logo/ and /logo/feed/";
$test1=preg_replace('/\/(\s|$)/',"$1",$test);
echo "$test1";
?>

SaishuHane
04-10-2012, 05:02 PM
I'd like it to match both /logo/ and /logo/feed/ . The restriction is that I only have two fields to use; an input and an output box. So I have it look for /logo/ and output /logo, but also look for /logo/feed/ and output /logo/feed

I was hoping that ((.*)(/feed)?) could be output as $1 so I could avoid doing something like ($2)? <--- (I don't even know if that would work. Probably not).

EDIT (for more clarity)

james438
04-10-2012, 05:27 PM
Does my PCRE not work in your case?

SaishuHane
04-10-2012, 05:51 PM
Regretfully, no. The thing I'm using is a plugin for 301'ing pages and it only accepts short regex's like the ones I posted. If there's a php alternative to 301'ing, I'd be more than interested! If I should have posted this elsewhere, my bad, but doing a search for "regex" on the site showed more people regex-related questions here.

james438
04-10-2012, 06:25 PM
I am not sure what 301'ing is. The regex I showed you was shorter than the one you showed.

SaishuHane
04-10-2012, 06:32 PM
301 as in a permanent redirect. http://en.wikipedia.org/wiki/HTTP_301

Sorry again, I should have been more clear. The regex seems to work in the way I need it to, but I'm not sure how to apply it in the way I need as I only have the two boxes to work with. The best I can gather from your code is:

Input: /\/(\s|$)/

Output: $1

Would this be correct?

SaishuHane
04-10-2012, 07:14 PM
Thank you so much! The $ in your regex gave me the idea to write (.*)/$ to blanket the entire site instead of writing one specifically for category and then tag. Many thanks for putting up with my unclear statements!

james438
04-10-2012, 09:30 PM
Glad I was able to help :)

traq
04-11-2012, 03:23 AM
If there's a php alternative to 301'ing, I'd be more than interested!

actually, the most appropriate place to do this would be Apache (in your .htaccess file). Your plugin is either writing to your .htaccess file dynamically (which can be error-prone), or simulating the same functionality in PHP (which would be very inefficient by comparison).

This is how the rule looks if you were to use it directly in .htaccess:
RewriteRule (.*)/$ $1 [L,R=301]If you try this, save your current .htaccess file first so you can restore it quickly and easily if anything goes wrong.

If you make a bad rule (or even just a typo), you can make your whole site inaccessible (but just restore the original .htaccess and you're fine).