I have many redirects in the .htaccess file. Now, I need to create a rewrite rule for a URL based on it's querystring value..
http://www.mydomain.com/subdir/RentalDetails.aspx?RentalId=1072
any traffic to this url should go to...
http://www.mydomain.com/subdir/RentalDirectory.aspx
I have researched and found some good posts on the subject and came up with this..
#discontinued properties
RewriteCond %{REQUEST_URI} ^/RentalDetails\.aspx$
RewriteCond %{QUERY_STRING} ^RentalId=1072$
RewriteRule ^(.*)$ /subdir/RentalDirectory.aspx? [R=301,L]
Unfortunately, it is not working. Could someone please help me to figure out where I have gone wrong? Many thanks in advance!
I'd guess that the RewriteCond is wrong: the uri shouldn't look like
^/RentalDetails\.aspx$
[Edited -- I finally managed to get to the actual docs, and found the query string was a red herring. However, I missed the '^' at the start of the string as well, which may be causing this]
Since the ^ at the start of the expression means "the string must start here", and the $at the end means "the string ends here", your regex is too restrictive, and won't match what you need it to.
From the apache docs, the REQUEST_URI variable excludes the query string, so that's not the issue. However, you are trying to match a url of /subdir/RentalDetails.aspx with a regex that has to start with the word /RentalDetails
Try something like
# No ^ at the start, this will match RentalDetails.aspx in any location
/RentalDetails\.aspx$
Or
# This will only match in the /subdir/ directory.
^/subdir/RentalDetails\.aspx$
Related
I have a lot of old urls inbound pointing to incorrect locations, trying to forward to new location. These are going to the root directory so I can't just forward everything.
One way to get a good chunk of them on to the new place is finding ones with a session ID in the query string. It always has 32 characters, preceded by s=
https://www.example.com/some-url-name-1233/?s=ba4a8a734b666b8d43499e5d497599a6
Need to move that to (and drop the session ID)
https://www.example.com/newfolder/some-url-name-1233/
I can't get the .htaccess redirect to match that string.
I've tried multiple ways, most recent being:
RewriteRule ^(.*)s=([^.]{32})$ https://www.example.com/newfolder/$1 [L,R=301]
Any suggestions?
This is an often answere, fully documented issue: you cannot access a request's query string by means of a RewriteRule. You need to use a RewriteCond for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^s=[^&]{32}(&|$)
RewriteRule ^ https://www.example.com/newfolder%{REQUEST_URI} [L,R=301,QSD]
I also fixed some other details.
I am trying to do the following:
User visits URL with query parameter: http://www.example.com/?invite=1234
I then want them to be deep linked into the app on their iOS device, so they go to: app_name://1234
Any suggestions on how to accomplish this in my .htaccess file?
I tried this but it doesn't work:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^invite/(.*)/$ app_name://$1 [NC,L]
If RewriteRule won't work, can anyone send me an example code for RewriteCond or JavaScript to achieve what I need?
Not sure how this will work with the iOS device, but anyway...
RewriteRule ^invite/(.*)/$ app_name://$1 [NC,L]
This doesn't match the given URL. This would match a requested URL of the form example.com/invite/1234/. However, you are also matching anything - your example URL contains digits only.
The RewriteRule pattern matches against the URL-path only, you need to use a RewriteCond directive in order to match the query string. So, to match example.com/?invite=1234 (which has an empty URL-path), you would need to do something like the following instead:
RewriteCond %{QUERY_STRING} ^invite=([^&]+)
RewriteRule ^$ app_name://%1 [R,L]
The %1 backreference refers back to the last matched CondPattern.
I've also restricted the invite parameter value to at least 1 character - or do you really want to allow empty parameter values through? If the value can be only digits then you should limit the pattern to only digits. eg. ^invite=(\d+).
I've include the R flag - since this would have to be an external redirect - if it's going to work at all.
However, this may not work at all unless Apache is aware of the app_name protocol. If its not then it will simply be seen as a relative URL and result in a malformed redirect.
I know there are lots of questions about this topic and i tried a lot of answers, now i found onw working, but the flexibility is missing
i got a folder strucure like
profile/user/username
now i want the the url to be shortened to
/username
the working version i got is this one:
RewriteCond %{REQUEST_URI} !^/profile/user/specialuser [NC]
RewriteRule ^specialuser/(.*)$ /profile/user/specialuser/$1 [L]
so using exactly this url, /profile/user/specialuser is transformed to /specialuser, but how can i keep it flexible, that [specialuser] is a placeholder for all the upcoming usernames?
The answer is pretty much there already. You use another capture group, and another placeholder in the rewritten string that is replaced by that capture group. The condition seems useless to me, so I removed it.
RewriteRule ^([^/]+)/(.*)$ /profile/user/$1/$2 [L]
[^/] matches a character that is not the / character. It is to make sure it matches only the first part of the url.
Problem: Visitors open the url website.com/?i=133r534|213213|12312312 but this url isn't valid anymore and they need to be forwarded to website.com/#Videos:133r534|213213|12312312
What I've tried: During the last hours I tried many mod_rewrite (.htaccess) rules with using Query_String, all failed. The last message in this topic shows a solution for this problem, but what would be the rule in my situation.
I'm very curious how you would solve this problem :)!
The following will handle the simple case you show. You'll need to add additional logic if you need to allow for other parameters in the query string or file names before the ?.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^i=(.*)
RewriteRule ^.* /#Video:%1? [NE,R=permanent]
Why is this tricky?
RewriteRule doesn't look at the query string, so you have to use RewriteCond to evaluate the QUERY_STRING variable and capture the part you'll need later (referenced via %1)
the hash character (#) is normally escaped, you must specify the [NE] flag
The trailing ? on the substitution string is required to suppress the original query string
I tested this on Apache 2.2.
Could anyone explain the following line please?
RewriteRule ^(.*)$ /index.php/$1 [L]
The parts of the rewrite rule break down as follows:
RewriteRule
Indicates this line will be a rewrite rule, as opposed to a rewrite condition or one of the other rewrite engine directives
^(.*)$
Matches all characters (.*) from the beggining ^ to the end $ of the request
/index.php/$1
The request will be re-written with the data matched by (.*) in the previous example being substituted for $1.
[L]
This tells mod_rewrite that if the pattern in step 2 matches, apply this rule as the "Last" rule, and don't apply anymore.
The mod_rewrite documentation is really comprehensive, but admittedly a lot to wade through to decode such a simple example.
The net effect is that all requests will be routed through index.php, a pattern seen in many model-view-controller implementations for PHP. index.php can examine the requested URL segments (and potentially whether the request was made via GET or POST) and use this information to dynamically invoke a certain script, without the location of that script having to match the directory structure implied by the request URI.
For example, /users/john/files/index might invoke the function index('john') in a file called user_files.php stored in a scripts directory. Without mod_rewrite, the more traditional URL would probably use an arguably less readable query string and invoke the file directly: /user_files.php?action=index&user=john.
That will cause every request to be handled by index.php, which can extract the actual request from $_SERVER['REQUEST_URI']
So, a request for /foo/bar will be rewritten as /index.php/foo/bar
(I'm commenting here because I don't yet have the rep's to comment the answers)
Point #2 in meagar's answer doesn't seem exactly right to me. I might be out on a limb here (I've been searching all over for help with my .htaccess rewrites...), and I'd be glad for any clarification, but this is from the Apache 2.2 documentation on RewriteRule:
What is matched?
The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string. If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
To me that seems to say that for a URL of
http: // some.host.com/~user/folder/index.php?param=value
the part that will actually be matched is
~user/folder/index.php
So that is not matching "all characters (.*) from the beggining ^ to the end $ of the request", unless "the request" doesn't mean what I thought it does.