Assuming a string like http://domain.com/aaaa/bbb/ccc/ddd
I want to use a .htaccess file to get the last element, in this case ddd.
I am using:
RewriteRule (.*)/$ ?pt=$1 [L]
But it only works with a trailing slash.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That’s because your regex includes a trailing slash.
Try:
The question mark after the slash means “zero or one”. If you don’t want that behavior, change the regex accordingly.
Edit:
Because the rewrite directives will be re-run after a rewrite is done (at least in per-directory context), it is important to ensure that a rule will not be re-run after you have already achieved the desired rewrite. There are multiple ways to accomplish this goal, but one is:
The point of the
RewriteCondis to prevent theRewriteRulefrom being applied when there is already a query string withptin it somewhere, which would be a clue that the rewrite has already happened. More complicated rules might require more or differentRewriteConddirectives.