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
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
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
Related
I am redirecting certain urls with path to get variables like the following:
localhost2/post/myTitle => localhost2/post.php?title=myTitle
localhost2/post/123 => localhost2/post.php?id=123
So In my htaccess file, I use
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^post/(\d+) post.php?id=$1
RewriteRule ^post/(.*) post.php?title=$1
</IfModule>
This works no problem. But I want to learn how to write negative of ^post/(\d+), that is ^post/(NEGATE-ONLY-NUMBERS). In other words I want a regex that matches the whole input sting if there is not only numbers after post/. So post/abc, post/a23, post/ab3, post/12c and post/a2c should all pass but not post/123. I refered to this post, which suggest using:
(?!^\d+$)^.+$
I can't use ^post/(?!^\d+$)^.+$, because there can be only one ^ and one $. I don't know what regex anchor specifies first position in a substring. My best guess is
post\/(?!\d++).*
I think (?!\d++), with the ++ would eat all characters followig and check if all are digits. But this fails at post/1ab.
Another guess is:
post\/(?![\d,\/]+$).*
The works the best but it allows: post/3455/X.
Secondly, eventually I need to convert localhost2/post/myTitle/123 => localhost2/post.php?title=myTitle&repeat=123 as well. I ave come up with the following:
^post/(?!\d+($|/))(.+?($|/))(\d+$)?
Note: +? to use lazy quantifier, otherwise multiple slashes will be matched by .
and
^post/(?!\d+($|/))([^/\n\r]+($|/))(\d+$)?
Here I use [^/\n\r] instead of .+?
Patterns inside zero-width assertions like (?!\d++) are non-consuming, they do not "eat" chars, they only check the context while keeping the regex index at the same location as before matching the zero-width assertion pattern.
You can use any of the following:
^post/(?!\d+(?:/|$)).*
^post/(?!\d+(?=/|$)).*
^post/(?!\d+(?![^/])).*
See the regex demo. Details:
^post/ - start of input, post/ literal string
(?!\d+(?=/|$)) - a negative lookahead that fails the match if, immediately to the right of the current location, there are one or more digits followed with / or end of string
.* - the rest of the input.
Do not over complicate things when you can keep things simple by keeping 3 separate rewrite rules and since your query parameters are named differently you will need 3 separate rewrite rules anyway.
Consider:
Options -MultiViews
RewriteEngine On
RewriteRule ^post/(\d+) post.php?id=$1 [L,QSA,NC]
RewriteRule ^post/([^/]+)/(\d+) post.php?title=$1&repeat=$2 [L,QSA,NC]
RewriteRule ^post/([^/]*) post.php?title=$1 [L,QSA,NC]
Take note of Options -MultiViews. If this is not enabled in Apache config you must have it here otherwise it will keep all $_GET parameters empty in your php file.
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.
What's the best way to rewrite URLs as 301 redirects with the following conditions?
Sample old URLs to rewrite:
/c/garments-apparel/red-yellow-polka-dress-10_450
/c/shoes-and-accessories/black-suede-boots-02_901
Conditions:
Change c to category
Remove trailing number (including connecting dash) from URL (example: -10_450 and -02_901)
New URLs should be:
/category/garments-apparel/red-yellow-polka-dress
/category/shoes-and-accessories/black-suede-boots
Note that changes will be applied to an .htaccess file on a Wordpress environment.
You can have this rule just below RewriteEngine On line:
RewriteEngine On
RewriteRule ^c/([\w-]+/.+)-[\d_]+/?$ /category/$1 [L,NC,R=301]
you can use the regex
[-_]\d+
to replace the trailing numbers with "" (empty string) demo
then use the regex
\/c\/
and replace with /category/ demo
I'm trying to make it so that if the ending of a URL contains anything surrounding a number (except that the first part can be any combination of numbers, a hyphen or a p), then the url is redirected with whatever surrounding the number is taken off.
Here's my regex:
RewriteRule ^all/[^p^P^0-9^\-]+([0-9]+).*$ /allof/$1 [R=301,NC,L]
If I tried these test URLs, the redirect should happen, but does not:
http://example.com/all/-a*1
http://example.com/all/plus100
If I tried this test URL, the redirect does not happen which is correct:
http://example.com/all/p1-100
If I tried these test URLs, the redirect happens, which is correct:
http://example.com/all/(100) - redirects to http://example.com/allof/100
http://example.com/all/minus100 - redirects to http://example.com/allof/100
Perhaps my regex is faulty. I tried removing the extra carets in the square brackets except for the first, and that didn't help, and I don't want to replace the square brackets with only a .* since I then won't be able to capture the number. What could I be doing wrong?
You can use negative lookahead in your rule:
RewriteRule ^all/(?!p\d*-)\D*(\d+) /allof/$1 [R=301,NC,L]
RegEx Demo
I want to have a regex that will validate url in following way
http://www.google.com ->valid url
www.google.com ->valid url
google.com ->in-valid url
http://google.com ->in-valid url
I have tried following regex from here
/(?:https?:\/\/)?(?:[a-zA-Z0-9.-]+?\.(?:[a-zA-Z])|\d+\.\d+\.\d+\.\d+)/
but it doens't validate the existence of www
You can use this regex in PHP:
$re = '~\b(?:https?://)?(www\.[^.\s]+\.[^/\s]+|\d+\.\d+\.\d+\.\d+)~';
RegEx Demo
This regex will enforce www. after optional htpp:// or https:// before it.
Another option in PHP is to use parse_url function and check for result['host'] array entry.
Put www in your regex:
/^(?:https?:\/\/)?(www(\.[\w-]+)+|\d+(\.\d+){3})/
I also improved it a bit (so it doesn't match "www......" etc)
See live demo.
To match www you will have to use regex like this.
Regex: (?:https?:\/\/)?www\.[a-zA-Z0-9.-]+.[a-zA-Z]+|(?:\d\.?){4}
Regex101 Demo
Hi I want to create a rule to remove the first directory on the url, see the example:
request url: http://www.example.com/San-Salvador/help
I want to redirect that to
Target url: http://www.example.com/help
the pattern is [base url][city name][directory] and I want to recreate it as this [base url][directory name]
Here is a general regex which should work:
^(http:\/\/www\.example\.com\/)(.*\/)(.*)
Each term in parentheses is a group which will potentially match an input string. For the input string:
http://www.example.com/San-Salvador/help
here are the matching groups:
http://www.example.com/
San-Salvador/
help
The groups you want to retain are the first and third ones, i.e. http://www.example.com/ and help to give you http://www.example.com/help
You can explore this regex here at Regex 101.
For the most part this is a rule that you can use to remove the city. However your code will need to handle what happens after the redirected URL is requested. Meaning what is displayed when this is called http://www.example.com/help
RewriteEngine on
RewriteRule ^(?:[^/]+)/([^/]+)/?$ /$1 [L]