2

I'm trying to use my .htaccess to pass both an id and a mode. Currently its passing an id and looks like this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([0-9]+)/?$
RewriteRule .* /listing.php?id=%1 [L,QSA]

The link looks like this:

http://www.drlistings.com/villas/stunning_180_degree_ocean_view/4074/

I want to add a mode, such as:

http://www.drlistings.com/villas/stunning_180_degree_ocean_view/4074/inline

to pass

listing.php?id=%1&mode=%2

I've examined the other posted questions but theirs seem a lot more complex than what I am doing. The second parameter doesn't have to be numeric, although if its easier to do it numeric I'm fine with that. It can be before or after the id paramater, i'm flexible, such as:

http://www.drlistings.com/villas/stunning_180_degree_ocean_view/inline/4074/

or if it were numeric it would be a 0/1 flag situation:

http://www.drlistings.com/villas/stunning_180_degree_ocean_view/0/4074/

Thanks!

1
  • Will the mode always be present, or is it optional? Commented Jul 26, 2014 at 21:51

1 Answer 1

1

Your initial rule should probably be simplified to handle the matching in RewriteRule instead of RewriteCond, as that would be a more conventional usage. Just to correct your existing rule, use

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match the number in $1 from the RewriteRule's left side
RewriteRule ^.+/([0-9]+)$ /listing.php?id=$1 [L,QSA]

To optionally accept the mode as an alphanumeric string following the id, you may add a non-capturing group (?:) in which a regular capture group is nested, which will populate the mode into $2. The whole thing is made optional by ending it with ?. Then a /? is appended to the end allowing for an optional trailing /.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/([0-9]+)(?:/([a-z0-9]+))?/?$ /listing.php?id=$1&mode=$2 [L,QSA]

Breaking it down:

  • ^[^/]+ matches the start of the string up to the first /
  • ([0-9]+) captures the numeric id into $1
  • (?: )? is a non-capturing group made optional by the ending ?, needed to allow for the possible absence of the mode
  • ([a-z0-9]+) matches an alphanumeric mode into $2
  • /? an optional trailing slash
  • $ end of the string.
Sign up to request clarification or add additional context in comments.

4 Comments

Works perfetctly.. and if there is no mode specified it gracefully ignores it.
@MichaelDeMutis Glad to hear it. Happy to help.
Michael, in this situation there is 2 parent folders, ie: /parent1/parent2/4074/ .. How would the same work when there is only one. ie. /parent1/4074/ and again with the ability to have /parent1/4074/inline/
@MichaelFever If you are using the second suggestion, it looks like currently it would only work with one parent to begin with. To make it work with 1 or more, it would be RewriteRule ^(?:[^/]+)+/([0-9]+)(?:/([a-z0-9]+))?/?$` wrapping the first [^/]+ expression in (?:)+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.