redirecting a dynamic url containing '?' mark - apache

My .htaccess file looks like this.
RewriteEngine On
RewriteRule (.*)-(.*)-(.*) view.php?title=$1&date=$2&author=$3
Using this rule i am able to redirect simple dynamic url's without special characters
domain.com/abcd-June%2027th%202013-authorname
to
domain.com/view.php?title=abcd&date=June%2027th%202013&author=authorname
Similarly, I want to redirect dynamic url's containing question mark '?' in query string
for example
http://domain.com/abcd?-June%2027th%202013-authorname
to
http://domain.com/view.php?title=abcd?&date=June%2027th%202013&author=authorname
How to do this? What are the changes required in .htaccess file?

Use NE flag.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^-]+)-([^-]+)-(.*?)/?\s
RewriteRule ^ view.php?title=%1&date=%2&author=%3 [L,QSA,NE]

Related

How to redirect URL using .htaccess with dynamic parameter?

I want to redirect
https://example.com/product-info/A100001
to
https://example.com/product-info/index/index/id/A100001
using htaccess redirect rule
A100001 will be dynamic like
A100001
A100002
A100003
A100004
....
I am trying this
RewriteEngine on
RewriteCond %{QUERY_STRING} product-info/A100001
RewriteRule ^$ /routing/index/index/id/? [L,R=301]
Source
Also tried other example but not working in my scnario
Anyone who expert in htacees rules can help me in this.
RewriteCond %{QUERY_STRING} product-info/A100001
RewriteRule ^$ /routing/index/index/id/? [L,R=301]
Your example URL contains a URL-path only, it does not contain a query string. The rule you've posted would redirect /?product-info/A100001 to /routing/index/index/id/.
Try something like the following instead:
RewriteRule ^(product-info)/(A\d{6})$ /$1/index/index/id/$2 [R=302,L]
The above would redirect a request of the form /product-info/A123456 to /product-info/index/index/id/A123456.
The $1 backreference simply contains product-info, captured from the RewriteRule pattern (saves repitition) and $2 contains the dynamic part (an A followed by 6 digits).
This is a 302 (temporary) redirect. Always test first with a 302 to avoid potential caching issues.
The order of directives in your .htaccess file is important. This rule will likely need to go near the top of the file, before any existing rewrites.
UPDATE:
redirection is working with your code, Can you please let me know the parameter pattern, I need the number from A452218 to A572217
Regex does not handle numeric ranges, only character ranges. If you specifically only want to match numbers in the stated range then you would need to do something (more complex) like this:
RewriteRule ^(product-info)/A(45221[89]|4522[2-9]\d|452[3-9]\d{2}|45[3-9]\d{3}|4[6-9]\d{4}|5[0-6]\d{4}|57[01]\d{3}|572[01]\d{2}|57220\d|57221[0-7])$ /$1/index/index/id/A$2 [R=302,L]
NB: The $2 backreference now only contains the dynamic number, less the A prefix, which is now explicitly included in the substitution string.

Redirect from PHP file to just query string

I want to use mod rewrite to redirect the following dynamic URL with h query string:
https://eu1.domain.com/~username/test.php?h=String_112016
To the following URL without the test.php part:
https://eu1.domain.com/~username/String_112016
I tried the following in my htaccess file but it's getting 404:
RewriteEngine On
RewriteRule ^\~username/([^/]*)$ /~username/test.php?h=$1 [L]
I don't have any other rule in the htaccess file.
From the code you wrote, I'm assuming what you want is not actually a redirect. Correct me if I'm wrong, but what you want is that when a user requests /~username/String_112016, you want the test.php page to be rendered and passed "String_112016" as a parameter labeled h.
You're really really close... Try this:
RewriteEngine On
RewriteRule ^/?~username/([^/]*)$ /~username/test.php?h=$1 [L]
The first matched character in the Rewrite Rule should be a forward slash, not a backslash. This forward slash is practically always present in requests, but I think it's best practice to treat it as optional, because technically you could get a (malformed) request that does not have one. The question mark after the /, makes the / optional.
RewriteEngine On
RewriteBase /
RewriteRule ^~username/test.php\?h=(.*)$ /~username/$1 [L]

htaccess redirect of URL's containing query

I have one URL, domain.com/fun/page/ which i want to redirect to itself when query string is appended to it (domain.com/fun/page/?xyz).
Server is LiteSpeed and no matter what (I've tried numerous rules found here and elsewhere on the web), I don't get what I want. Seems I'm just not skilled in writing matching regex.
put this code in your /fun/.htaccess file:
RewriteEngine On
RewriteBase /fun/
RewriteCond %{QUERY_STRING} .+
RewriteRule ^page/?$ %{REQUEST_URI}? [NC,R=302,L]
Trailing ? will strip off any existing query string.

apache rewrite url for codeigniter

I need to build app and i'm using codeigniter for this.
Company sends me a link in format that cant be changed from their side
Link is like:
someurl.com/sms.php?phone=12345678&msg=msg15&code=777&country=cc&oper=someoper& mssid=1234567892&notcharged=0&date=2011-12-26+23%3A31%3A27&keyword=msg&created=2011-12-26+23%3A31%3A26
How can i rewrite it with .htaccess so the codeigniter gets link in segmented format like
someurl.com/sms/myfunction/12345678/msg15/777/cc/someoper/1234567892/0/2011-12-26+23%3A31%3A27/msg/2011-12-26+23%3A31%3A26
Thanks
Edit.
Spent some hours and tried something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^phone\=([^&]+)\&msg\=([^&]+)\&code\=([^&]+)\&country\=([^&]+)\&oper\=([^&]+)\&mssid\=([^&]+)\&date\=([^&]+)\&keyword\=([^&]+)$
RewriteRule ^test\.php$ /sms/doParse/%1/%2/%3/%4/%5/%6/%7/%8 [R,L]
And then typing url like this:
http://test.airtel.lv/test.php?phone=12345678&msg=msg15&code=777&country=cc&oper=someoper&mssid=1234567892&date=2011-12-26+23%3A31%3A27&keyword=msg
I got this:
http://test.airtel.lv/sms/doParse/12345678/msg15/777/cc/someoper/1234567892/2011-12-26+23%253A31%253A27/msg?phone=12345678&msg=msg15&code=777&country=cc&oper=someoper&mssid=1234567892&date=2011-12-26+23%253A31%253A27&keyword=msg
Why returned url has GET params in the ending ?
And one more question, if that company will change and add extra param to link, the rewriting will be broken ? Then how can it be more universal ?
Try this placing this in your .htaccess file in an appropriate place in your document root:
RewriteEngine on
RewriteRule ^sms.php$ /sms/myfunction/ [L]
RewriteCond %{QUERY_STRING} ^[^=]+=([^&]+)&?(.*)
RewriteRule ^sms/myfunction/(.*)$ /sms/myfunction/$1/%1?%2 [L]
The first RewriteRule changes the /sms.php part of the URI to /sms/myfunction/ and the second rule will append each value in your query string into its own part of the path. So a url like this: http://someurl/sms.php?a=1&b=2&c=3&d=4&e=5&f=6&g=7&h=8&i=9 will have the URI internally rewritten to /sms/myfunction/1/2/3/4/5/6/7/8/9. There's no checks on what the variable names are in the query string, only the value is extracted and appended to the URI path.
If you put that code in your server or vhost config (instead of an htaccess file) add a / in front of the sms in each of the rules so it says ^/sms because rules in the htaccess have the leading slash stripped off when matching against the URI.
just handle the GETs in your controller. it's okay, GET isnt evil.
parse_str($_SERVER['QUERY_STRING'], $_GET);
then you can have a simpler rewrite to handle the special route

Redirect If URL Param Contains Specific Substring Value

I'm trying to use mod_rewrite to redirect users coming with a certain substring in their URL.
For example, INBOUND_XXX34_MPEG, if 34 is in the URL, I need to redirect to a different integer (i.e.: INBOUND_XXX20_MPEG)
Is this type fine tooth-combing possible with mod_rewrite?
EDIT what I have so far, but fails in testing:
RewriteEngine on
RewriteRule ^(.*=\w*)34(.*)$ $120$2
Solved it!
RewriteCond %{QUERY_STRING} linkid=(.*)34(_.*)$
RewriteRule (.*) $1?linkid=%120%2 [R=301,L]
This will preserve URI, additional query params, and target the substring index.
I'm not sure about what you really want but how about this:
RewriteRule ^(.*)INBOUND_XXX34_MPEG(.*)$ $1INBOUND_XXX20_MPEG$2
I do not know if the XXXis some can of variable thing?