Yasir
Yasir

Reputation: 3907

get numbers from url using regex

i want to page redirection and unable to write condition for it

have different scenario want to redirect friendly url to query string base

http://www.domainname.com/directoryname/friendly-url-goes-here_123456.html

friendly-url-goes-here can be like this friendly_url-goes_23-here_123456.html, i just want 123456

and page get redirected to this

http://www.domainname.com/detail-page?id=123456

123456 will be a variable

Upvotes: 1

Views: 65

Answers (2)

anubhava
anubhava

Reputation: 786091

You can use this lookahead based regex:

\d+(?=\D*$)

RegEx Demo

.htaccess:

Inside your root .htaccess you can use this rewrite rule:

RewriteEngine On

RewriteRule ^directoryname/.*?(\d+)\D*$ /detail-page?id=$1 [L,QSA,NC,R]

Reference: Apache mod_rewrite Introduction

Apache mod_rewrite Technical Details

Apache mod_rewrite In-Depth Details

Upvotes: 4

Dalorzo
Dalorzo

Reputation: 20024

May be this could be one way to do it:

(\d+).html$
  • \d+ match a digit [0-9]

Online Demo 1

and this is another way:

\d+(?=\.html$)
  • (?=.html$) It is a positive lookahead - it assert that the regex below can be matched if it contains at end($) a .html

Online Demo 2

Upvotes: 0

Related Questions