I'm having issue making this redirection works. It seems like the issue come from the question mark ?page= in the RewriteCond's line. But can't find a way to escape the question mark.
RewriteCond %{REQUEST_URI} ^/v/models/?page=([a-z\-A-Z0-9]+)$ [NC]
RewriteRule . models.php?category=dog&page=%1 [L,QSA]
Any help?
RewriteCond %{REQUEST_URI} ^/v/models/$ [NC]
RewriteRule . models.php?category=dog [L,QSA]
Adding QSA made it work (Query String Append)
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 want to redirect this page with htaccess
products_filter.php?f16%5B0%5D=bla+bla+bla&cPath=72&M_ID=12x
to
products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x
i tried this (and many other ways)
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla$
RewriteRule ^products_filter\.php$ http://www.example.com/products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x [L,R=301]
what am i doing wrong here?
Problem is using $ (end of input) in this regex:
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla$
Since your query string is: 16%5B0%5D=bla+bla+bla&cPath=72&M_ID=12x
Change that line to:
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla(&|$)
Update:
Looking at your question I realize that you are using quite a few special characters that need to be escaped.
RewriteCond %{QUERY_STRING} ^(f16%5B0%5D)=bla\+bla\+bla(?:&(.*)|$) [NC]
RewriteRule ^(products_filter\.php)$ /$1?%1=bla+bla&%2 [L,R=301,NE]
PS: It is important to use NE flag here. Otherwise %5B and %5D will be further encoded by Apache.
Try this code :
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla
RewriteRule ^products_filter\.php$ http://www.example.com/products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x [L,R=301]
I am trying to make:
http://www.specialisedorthoticservices.co.uk/image.php?object_type=detailed&image_id=140&window=popup
become this:
http://www.specialisedorthoticservices.co.uk
The result of the query no longer exists and the re-direct doesn't seem to work see code below:
RewriteCond %{REQUEST_URI} ^/image\.php$
RewriteCond %{QUERY_STRING} ^object_type=detailed&image_id=140&window=popup$
RewriteRule ^(.*)$ http://www.specialisedorthoticservices.co.uk [R=301,L]
Remove the slash before image.php. In fact, why not just shorten it? To not append the query string you need a terminating question mark like so
RewriteEngine On
RewriteCond %{QUERY_STRING} ^object_type=detailed&image_id=140&window=popup$
RewriteRule ^image\.php$ /? [R=301,L]
I want users who type
http://www.example.com/word-of-the-day
to be taken to
http://www.example.com/index.php?page=word-of-the-day
But I want
http://www.example.com/word-of-the-day
to be shown in the URL for the user.
What should I do in my .htaccess? I tried but the regular expression
and the syntax of RewriteRule is way too complicated for me to
figure out how to do it.
Any help will be appreciated.
Edit:
Also, how can I say this in htaccess -
if they type http://www.example.com/word-of-the-day, take them to http://www.example.com/index.php?page=word-of-the-day
or if they type http://www.example.com/something-else, take them to http://www.example.com/index.php?page=something-else
or else, just take them to the URL they typed.
The condition below checks that index.php is not being requested. If not apply the rule. This will work for any of the scenarios you listed above.
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L]
In response to your comment about only wanting to do this for a few specific pages, it would look like this(as an alternative to Nils edit):
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteCond %{REQUEST_URI} ^word-of-the-day$ [OR]
RewriteCond %{REQUEST_URI} ^something-else$ [OR]
RewriteCond %{REQUEST_URI} ^even-something-else$
RewriteRule ^(.*)$ index.php?page=$1 [L]
Try this
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
Or more flexible
RewriteEngine on
RewriteRule ^(.*)$ index.php?page=$1
Not tested, yet it sould work.
To your edit:
Just define those specific URLs manually:
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
RewriteRule ^word-some-example$ index.php?page=some-example
RewriteRule ^some-other$ index.php?page=some-other
How could I use a rewrite to change:
/?tag=foo
To:
/tag/foo
I tried:
RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/$1 [L]
But it did not work.
Try the following:
RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/%1 [L]
Usually, rewrites are used to achieve the opposite effect. Are you sure you don't really want to do the following?
RewriteRule ^tag/(.+)$ index.php?tag=$1 [L]
To avoid recursion, you should check the request line instead as the query string in %{QUERY_STRING} may already have been changed by another rule:
RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)tag=([^&\s]+)&?([^\s]*)
RewriteRule ^(.*)$ /tag/%3?%1%4 [L,R=301]
Then you can rewrite that requests back internally without conflicts:
RewriteRule ^tag/(.*) index.php?tag=$1 [L]
I was trying to convert from a URL like this:
http://java.scandilabs.com/faq?key=Contents_of__gitigno
To a URL like this:
http://scandilabs.com/technology/knowledge/Contents_of__gitigno
Andrew's response above worked for me with the addition of a question mark at the end to discard the original query string:
RewriteCond %{HTTP_HOST} ^java
RewriteCond %{QUERY_STRING} ^key=(.+)$
RewriteRule ^(.*)$ http://scandilabs.com/technology/knowledge/%1? [R=301,L]
Note if you're on apache 2.4 or higher you can probably use the QSD flag instead of the question mark.