I would like to change
detail.php?cid=$category&ccid=$category2&cccid=$category3&fid=$id&ftitle=$titleseo
to
detail/$category/$category2/$category3/$id/$titleseo
But in case one of the parameters equals 0, the parameter should not be visible.
So in case $category=0, the url should become:
detail/$category2/$category3/$id/$titleseo
In case $category2=0, the url should become:
detail/$category/$category3/$id/$titleseo
Is this possible with a rewrite rule in htaccess and if yes how should this look like?
You could use this considering /$category/$category2/$category3/ have given names /$id/$titleseo are fixed :
RewriteEngine On
RewriteBase /
RewriteRule ^detail/(\$category)?/?(\$category2)?/?(\$category3)?/?(\$id)/(\$titleseo)$ - [E=PASS:cid=$1&ccid=$2&cccid=$3&fid=$4&ftitle=$5]
RewriteCond %{ENV:PASS} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule ^ details.php?%1%2%3%4 [L]
RewriteCond %{QUERY_STRING} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule ^ details.php?%1%2%3%4 [L]
RewriteCond %{ENV:PASS} ^(.+)$
RewriteRule ^ details.php?%1 [L]
In general use this :
RewriteEngine On
RewriteBase /
RewriteRule ^detail/([^/]+)?/?([^/]+)?/?([^/]+)?/?(\$id)/(\$titleseo)$ - [E=PASS:cid=$1&ccid=$2&cccid=$3&fid=$4&ftitle=$5]
RewriteCond %{ENV:PASS} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule ^ details.php?%1%2%3%4 [L]
RewriteCond %{QUERY_STRING} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule ^ details.php?%1%2%3%4 [L]
RewriteCond %{ENV:PASS} ^(.+)$
RewriteRule ^ details.php?%1 [L]
The rules above , first make and environment by capturing any request start with details and ended with /$id/$titleseo then check that ENV values to see which one has/hasn't a value and finally redirect it accordingly.
Both rules work in case of three levels of categories if you need more you could edit the rules like this :
Add this ([^/]+)?/? after RewriteRule ^detail/ then [E=PASS:cid=$1&ccid=$2&cccid=$3&ccccid=$4&fid=$5&ftitle=$6] at the end , you could compare to see the difrenec between this and previous one to get a criteria , and finally add %5 to /details.php?%1%2%3%4 [L] like this /details.php?%1%2%3%4%5 [L] and so on .
Did not get the code from Mohammed to work so far, but used
RewriteRule ^detail/?([0-9a-zA-Z]+)?/?([0-9a-zA-Z]+)?/?([0-9a-zA-Z]+)?/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) detail.php?cid=$1&ccid=$2&cccid=$3&fid=$4&&ftitle=$5
Related
#anubhava provided an excellent answer for my previous question of doing an .htaccess internal rewrite with the below code, which worked for my one search query.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
I wanted to make this a separate question since my next question is slightly different. How could I adapt this to also work with two parameters? For instance, I would also like http://ipaddress/directory/file.php?id=47?name=value1 to redirect to http://ipaddress/directory/47/value1
name= can also be any combination of letters and numbers, like value1050, etc.
Thank you #anubhava for your previous answer above, and maybe there's a way to add on this second parameter as well?
Considering you are segregating your query string values in id=1234&name=value123 style, since passing 2 times query string will not be allowed, then you could try following, fix of your shown attempt.
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=(\d+)&name=(.*)$ [NC]
RewriteRule ^file\.php/?$ /directory/%1/%2? [R=301,L,NC]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [L,QSA,NC]
2nd solution: Adding 1 more solution here, either use above OR use following one at a time only please.
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/file\.php\?d=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]
I am trying to learn how to use RewriteRule.
I have 2 'pages' (wordpress)-
domain.com/webinars/ (shows list of all webinars)
and
domain.com/webinar/ (shows specific webinar details)
I have trying to set the following conditions -
(1) domain.com/webinars/{Year}/{Month}/ will load /webinar/?year={Year}&month={Month}
(2) domain.com/webinar/ (with no /{Year}/{Month}) will load /webinars/
(3) domain.com/webinar/?year={Year}&month={Month} will redirect to /webinars/{Year}/{Month}/ and then apply condition #1
This is my attempted code
RewriteEngine On
RewriteRule ^/webinars/([0-9]+)/([A-Za-z0-9]+)/$ /webinar/?year=$1&month=$2 [NC]
RewriteRule ^/webinar/$ /webinars/
RewriteRule ^/webinar/year=(\d+)$month=([\w-]+)$ /webinars/%1/%2? [R=301,L]
Condition #1 results in a 404 page not found
Condition #2 shows /webinar/ and not /webinars/
Condition #3 stays on domain.com/webinar/?year={Year}&month={Month} and does not redirect
What am I doing wrong? Only other code in my htaccess file is the default wordpress block.
Try the following :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^webinars/([^/]+)/([^/]+) /webinar/?year=$1&month=$2 [NC]
The rest could be done the same way .
Try with below rules,
RewriteCond %{REQUEST_URI} ^/webinar$
RewriteCond %{QUERY_STRING} ^year=([^/]+)&month=([^/]+)
RewriteRule ^ http://example.com/webinars/%1/%2/? [R=302]
RewriteCond %{REQUEST_URI} ^/webinars/([^/]+)/([^/]+)/$
RewriteRule ^ webinar/?year=%1&month=%2 [L,END]
RewriteCond %{REQUEST_URI} ^/webinar/$
RewriteRule ^ webinars/ [L]
You can use this
RewriteEngine on
# if /webinar/ is requested skip the rules and serve /webinar/ directory
RewriteRule ^webinar/?$ - [L]
#redirect /webinar/?year={year}&month={month}
#To /webinar/year/month
RewriteCond %{THE_REQUEST} /webinar/\?year=([^\s&]+)&month=([^\s&]+) [NC]
RewriteRule ^.+$ /webinar/%1/%2? [L,R]
# rewrite new url to the old one
RewriteRule ^webinar/([^/]+)/([^/]+)/?$ /webinar/?year=$1&month=$2 [L,NC]
I have the following lines in my .htaccess file:
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^page/([0-9]+)/$ ?page=$1 [L]
RewriteRule ^(20\d\d)/page/([0-9]+)/$ ?yearMeasure=$1&page=$2 [L]
RewriteRule ^(20\d\d)/$ ?yearMeasure=$1 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/page/([0-9]+)/$ ?category=$1&page=$2 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/$ ?category=$1 [L]
RewriteRule ^(20\d\d)/([A-Za-z0-9-]+)/$ ?type=post&year=$1&title=$2 [L,QSA]
When the file is left as above, the server does not capture /page/1/. Rather, it returns a 404 error. However, when I flip the second and third lines, everything works perfectly. Why is this?
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^(20\d\d)/page/([0-9]+)/$ ?yearMeasure=$1&page=$2 [L]
RewriteRule ^page/([0-9]+)/$ ?page=$1 [L]
RewriteRule ^(20\d\d)/$ ?yearMeasure=$1 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/page/([0-9]+)/$ ?category=$1&page=$2 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/$ ?category=$1 [L]
RewriteRule ^(20\d\d)/([A-Za-z0-9-]+)/$ ?type=post&year=$1&title=$2 [L,QSA]
Edit:
After a few minutes of experimenting, it seems that the line that comes directly after RewriteCond %{QUERY_STRING} id=([0-9]+) is ineffectual.
Any help is appreciated.
Any RewriteCond belongs to the RewriteRule directly following it
The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern, and if these conditions are met.
1. RewriteCond ...
2. RewriteRule ...
3. RewriteCond ...
4. RewriteRule ...
5. RewriteRule ...
So in this case condition 1 belongs to rule 2, and condition 3 belongs to rule 4, and finally rule 5 has no condition at all.
This means, when you have
1. RewriteCond ...
2. RewriteRule ...
3. RewriteRule ...
and switch rules 2 and 3,
1. RewriteCond ...
3. RewriteRule ...
2. RewriteRule ...
the condition belongs to the other rule.
When you use the first set of rules and request /page/1/?id=5, it would work, because now the preceding condition "QUERY_STRING matches id=([0-9]+)" is met.
I have an htaccess file with some rules, and I now want to add another rule which strips a part of the URL such that
www.mydomain.com/?generations/anything
becomes
www.mydomain.com/anything
(and anything means any other characters). I can make it work without the ?, but I can't seem to match/remove the ?. I tried:
1 failed:
RewriteRule ^\?generations/(.*)$ /$1 [L]
2 failed:
RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^generations/(.*)$ $1 [L]
3 failed:
RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^([^?]*)?generations/(.*)$ $2%1 [L]
4 failed:
RewriteRule ^([^?]*)?generations/(.*)$ $1$2 [QSA,L]
Can someone create a working rule - and explain why my third attempt above is not working? I can potentially see problems with the first two, but the third and fourth should work...
You're getting there with 3, but the query string is not included in a RewriteRule match. So you just want to match the empty string:
RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^$ %1? [L]
But you also have it the wrong way round I think, if you want /anything to be the URL that is visited in the browser. You actually want just this:
RewriteRule ^(.*)$ ?generations/$1 [L]
I would like to redirect, for example :
index.php?name=sarah
to
names/sarah.php
The folder structure:
names/...
index.php
I've tried :
RewriteRule index.php?name=$1 /names/$1.php
and other ones, but didn't came to a solution.
Is this possible? If so, how can you do this?
Note:
The other way round is
RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]
which works fine.
Here is your rule:
RewriteCond %{QUERY_STRING} name=([^&]*)
RewriteRule ^.*$ /names/%1.php?
You first example wont work as you are trying to manipulate query strings using a RewriteRule but this is not allowed. You can try the following :
RewriteEngine on
RewriteCond %{THE_REQUEST} / index\.php\?q=([^\s]+) [NC]
RewriteRule ^ /names/%1.php? [L,R]
RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]