Htaccess rewrite rule with 2 optional parameters - .htaccess

I have the page example.php with 2 optional parameters name1 & name2.
The page also works with no parameters.
This is my rewrite rule for both parameters included, but this rule won't work for the page without parameters. How do I do. please?
RewriteRule ^(example)/([^/]+)-vs-([^/]+)?$ $1.php?name1=$2&name2=$3 [NC,L,QSA]

Related

How do I rewrite an id to a name via htaccess

I am trying to rewrite the title ID through htaccess but it does not work. I used several different codes in htaccess but it does not work.
When I went to the url by name it did not show me any post, but with ID it works very well.
I am using this code in htaccess:
RewriteRule ^([0-9a-z]+)/?$ news-details.php?nid=$1 [NC,L,QSA]
ID for posts/categories:
127.0.0.1/new/news-details.php?nid=89
127.0.0.1/new/category.php?catid=9
Yes it works through the id "/new/89" you mentioned but not with url "/new/89/name-news"
Then change the rule (the regex) to allow /<id>/<slug> (no trailing slash), for example:
RewriteRule ^(\d+/[\w-]+)$ news-details.php?nid=$1 [NC,L,QSA]
<id>/<slug> is then past as the value of the nid URL parameter.

Rewrite Rule for Masking the URL with Query string and Page number

I am trying to mask the following url with any page number using htaccess.
https://sampledomain.com/?page=2
to
https://sampledomain.com/page/2
So if we call https://sampledomain.com/page/2 url in the website, internally path should always be calling https://sampledomain.com/?page=2 url
Please suggest me the correct htaccess rule.
In htaccess in your document root, use the following rule :
RewriteEngine on
RewriteRule ^page/([0-9]+)/?$ /?page=$1 [QSA,L]

Rewrite Rule with wildcard subdomain and optional extra parameters

I am trying to write rewrite rules to accept a subdomain as well as an optional page on the end of the domain, example:
a.domain.com
a.domain.com/1
Where "a" can be anything and the 1 is a numeric input.
I currently have the following conditions and rules:
RewriteCond %{HTTP_HOST} !www\.
RewriteCond %{HTTP_HOST} (.*)\.domain\.xyz
RewriteRule .* view.php?username=%1 [L]
which works with the subdomain, but cannot find a way to allow the second parameter on the end to work as well.
What rules can I add to make this work?

Htaccess URL redirect by matching url string and parameters

I need to match the URL format and depends on URL matches need to redirect the incoming requests to different pages.
For example
http://www.domain.com/path1/path2/ wrong-url -1/ ?var1=val1
http://www.domain.com/path1/path2/ another-wrong-url -1/ ?var1=val1
http://www.domain.com/path1/path2/ third-wrong-url -1/?var1=val1
http://www.domain.com/path1/path2/ fourth-wrong-url -1/?var1=val1
See the High lighted URL Matches. It always having -1 as the url string. . That needs to be redirected one static page.
And some other URL's always have var1 as URL Query String parameter. So if URL have var1 as Query string then those URL's needs to be redirected to another Static page.
So i tried this but didn't worked. Please help me in this redirecting script
RewriteEngine on
RewriteRule ^(path1/path2/[^-1]*)$ http://www.domain.com/target-page [L,R=301]
1. Rule for URL that ends with -1/:
RewriteRule ^path1/path2/([^/]+)-1/$ http://www.domain.com/target-page [L,R=301]
2. Rule for having var1= parameter in query string:
RewriteCond %{QUERY_STRING} (^|&)var1=([^&]*)(&|$)
RewriteRule .* http://www.domain.com/another-target-page [L,R=301]
NOTE:
With these rules existing query string will be passed to a new URL as well (e.g. /path1/path2/wrong-url-1/?say=meow will become http://www.domain.com/target-page?say=meow). To drop it, add ? at the end of target URL (e.g. http://www.domain.com/another-target-page? [L,R=301]

htaccess url masking mod rewrite

I have done some URL masking and it all works very nicely. I have one issue that I am trying to resolve now:
RewriteRule ^(.*)/(.*)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3
with the above I can access the same page 2 ways, www.domain.com/premier-league/2010-2011/arsenal/ and www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal
is there a way in my rewrite rule I can redirect the URL (301 ideally) is someone does it through the untidy way "www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal"?
Thanks in advance
If you add another rewrite rule after this one that matches your 'inverse' pattern, and mark both as the [L]ast rule, that might work. I first rewrite the url to include the query string, and [C]hain that one to the next rule. After that we [R]edirect the browser.
RewriteRule ^(.)/(.)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3 [L]
RewriteRule ^(.+) $1%{QUERY_STRING} [C]
RewriteRule ^teams.php?competition=([a-zA-Z0-9]+)&season=([a-zA-Z0-9]+)&teamid=([a-zA-Z0-9]+)$ $1/$2/clubs/$3/ [R=301,L]
Note I haven't tested this or the regex of the second rule. Might need to adjust the character ranges a bit. Also I haven't tested the query string rewrite in the second rule.
Edit: See here for some common use cases of mod_rewrite: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Resources