Results 1 to 10 of 10

Thread: Regular Expression and Trailing Slashes

  1. #1
    Join Date
    Aug 2006
    Location
    North Las Vegas, Nevada
    Posts
    24
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Regular Expression and Trailing Slashes

    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?
    Last edited by SaishuHane; 04-10-2012 at 07:15 PM.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    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:

    Code:
    <?php
    $test="/logo/feed/";
    $test = substr($test, 0, -1);
    echo "$test"; 
    ?>
    If the latter try:

    Code:
    <?php
    $test="/logo/ and /logo/feed/";
    $test1=preg_replace('/\/(\s|$)/',"$1",$test);
    echo "$test1"; 
    ?>
    Last edited by james438; 04-10-2012 at 04:57 PM.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. The Following User Says Thank You to james438 For This Useful Post:

    SaishuHane (04-10-2012)

  4. #3
    Join Date
    Aug 2006
    Location
    North Las Vegas, Nevada
    Posts
    24
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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)

  5. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Does my PCRE not work in your case?
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #5
    Join Date
    Aug 2006
    Location
    North Las Vegas, Nevada
    Posts
    24
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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.

  7. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I am not sure what 301'ing is. The regex I showed you was shorter than the one you showed.
    To choose the lesser of two evils is still to choose evil. My personal site

  8. #7
    Join Date
    Aug 2006
    Location
    North Las Vegas, Nevada
    Posts
    24
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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?

  9. #8
    Join Date
    Aug 2006
    Location
    North Las Vegas, Nevada
    Posts
    24
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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!

  10. #9
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Glad I was able to help
    To choose the lesser of two evils is still to choose evil. My personal site

  11. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by SaishuHane View Post
    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:
    Code:
    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).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •